SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I can not save form input to the database

    Hello,

    i have made a form and a php script to save the user input data to a database but after clicking the submit button only the auto increment primary key is added and all other field are blank the database was created with a php script and the table was created with a program call rekall (linux) and i am using easy eclipse (LAMP) for my designs

    the form code

    Code:
    <form action="add_done.php" method="post" enctype="text/plain">
    
      <table>
        <tr>
            <td><label>Name</label></td>
    		<td>
      		<input type="text" name="name" value="" size="40" /></td>
        </tr>
    
        <tr>
    		<td><label>telephone</label></td>
    		<td><input type="text" name="telephone" value="" size="40" /></td>
        </tr>
    
        <tr>
          	<td><label>Mobile</label></td>
    		<td><input type="text" name="mobile" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>E-mail</label></td>
    		<td><input type="text" name="email" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>building type</label></td>
    		<td>
      <select name="type" size="3">
        <option value="app">appartment</option>
        <option value="land">land</option>
    	<option value="villa">villa</option>
      </select>
      </td>
        </tr>
    
    	<tr>
          	<td><label>city</label></td>
    		<td>
      <select name="city" size="3">
        <option value="cairo">cairo</option>
        <option value="alex">alexandria</option>
        <option value="ism">ismailia</option>
      </select>
    </SELECT>
    	 </tr>
    
    		<tr>
          	<td><label>address</label></td>
    		<td><input type="text" name="address" value="" size="40" /></td>
        </tr>
    
        </tr>
    		<tr>
          	<td><label>area</label></td>
    		<td><input type="text" name="area" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>floors</label></td>
    		<td><input type="text" name="floors" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>advantages</label></td>
    		<td><input type="text" name="advs" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>finishing</label></td>
    		<td><input type="text" name="finish" value="" size="40" /></td>
        </tr>
    
        <tr>
          	<td><label>images(if found)</label></td>
    	<td><input type="file" name="file" size="50" maxlength="100000" accept="text/*"/></td>
        </tr>
    
    	<tr>
    	<td></td>
    	<td><input type="submit" value="submit"/></td>
    	</tr>
    
      </table>
    
    
      </form>
    the php script code
    PHP Code:
    <?php
    $con 
    mysql_connect("mepis","root","remote");
        if(!
    $con)
            {
                die(
    'Could not connect: 'mysql_error());
            }

    mysql_select_db("amaar",$con);
    $sql="insert into ads (name, telephone, mobile, email, type, city, district, address, area, floors, advs, finishing)
             values
                    ('
    $_POST[name]','$_POST[telephone]','$_POST[mobile]','$_POST[email]','$_POST[type]','$_POST[city]','$_POST[district]','$_POST[address]',
                            '
    $_POST[area]','$_POST[floors]','$_POST[advs]','$_POST[finishing]')";
    if(!
    mysql_query($sql,$con))
        {
            die(
    'error'mysql_error());
        }
    echo 
    "1 record added";
    mysql_close($con);

    ?>
    and i have uploaded a screen shot of how my database look like
    i would like to know what is wrong with my code because nearly an entire night was wasted to get it work but no luck at all

    any help is really appreciated and thanks in advance
    Attached Images Attached Images

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try a bit of diagnostic code.

    before executing the query add
    PHP Code:
    print_r($_POST);
    echo 
    "<br>$sql"
    What do you get from that?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Try a bit of diagnostic code.

    before executing the query add
    PHP Code:
    print_r($_POST);
    echo 
    "<br>$sql"
    What do you get from that?
    Thank you for your reply cranial-bore

    I inserted the your code before

    $sql="insert into ads ......"

    and the out put was
    Code:
    Array ( )
    1 record added
    what should i understand from this line?!

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this, it works for me
    Code:
    <form action="add_done.php" method="post" enctype="multipart/form-data">
    
      <table>
        <tr>
            <td><label>Name</label></td>
    		<td>
      		<input type="text" name="name" value="" size="40" /></td>
        </tr>
    
        <tr>
    		<td><label>telephone</label></td>
    		<td><input type="text" name="telephone" value="" size="40" /></td>
        </tr>
    
        <tr>
          	<td><label>Mobile</label></td>
    		<td><input type="text" name="mobile" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>E-mail</label></td>
    		<td><input type="text" name="email" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>building type</label></td>
    		<td>
      <select name="type" size="3">
        <option value="app">appartment</option>
        <option value="land">land</option>
    	<option value="villa">villa</option>
      </select>
      </td>
        </tr>
    
    	<tr>
          	<td><label>city</label></td>
    		<td>
      <select name="city" size="3">
        <option value="cairo">cairo</option>
        <option value="alex">alexandria</option>
        <option value="ism">ismailia</option>
      </select>
    </SELECT>
    	 </tr>
    	 
    	 <tr>
          	<td><label>district</label></td>
    		<td><input type="text" name="district" value="" size="40" /></td>
        </tr>
    
    		<tr>
          	<td><label>address</label></td>
    		<td><input type="text" name="address" value="" size="40" /></td>
        </tr>
    
        </tr>
    		<tr>
          	<td><label>area</label></td>
    		<td><input type="text" name="area" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>floors</label></td>
    		<td><input type="text" name="floors" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>advantages</label></td>
    		<td><input type="text" name="advs" value="" size="40" /></td>
        </tr>
    
    	<tr>
          	<td><label>finishing</label></td>
    		<td><input type="text" name="finishing" value="" size="40" /></td>
        </tr>
    
        <tr>
          	<td><label>images(if found)</label></td>
    	<td><input type="file" name="file" size="50" maxlength="100000" accept="text/*"/></td>
        </tr>
    
    	<tr>
    	<td></td>
    	<td><input type="submit" value="submit"/></td>
    	</tr>
    
      </table>
    
    
      </form>
    and the php
    PHP Code:
    <?php
    $con 
    mysql_connect("mepis","root","remote");
     if(!
    $con)
    {
         die(
    'Could not connect: ' mysql_error());
         }

    mysql_select_db("amaar"$con);
    $sql "insert into ads (name, telephone, mobile, email, type, city, district, address, area, floors, advs, finishing) 
             values 
                    ('
    $_POST[name]','$_POST[telephone]','$_POST[mobile]','$_POST[email]','$_POST[type]','$_POST[city]','$_POST[district]','$_POST[address]', 
                            '
    $_POST[area]','$_POST[floors]','$_POST[advs]','$_POST[finishing]')";
    if(!
    mysql_query($sql$con))
        {
         die(
    'error' mysql_error());
         }
    echo 
    "1 record added";
    mysql_close($con);

    ?>
    test sql
    Code:
    CREATE TABLE `ads` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(250) default NULL,
      `telephone` varchar(250) default NULL,
      `mobile` varchar(250) default NULL,
      `email` varchar(250) default NULL,
      `type` varchar(250) default NULL,
      `city` varchar(250) default NULL,
      `district` varchar(250) default NULL,
      `address` varchar(250) default NULL,
      `area` varchar(250) default NULL,
      `floors` varchar(250) default NULL,
      `advs` varchar(250) default NULL,
      `finishing` varchar(250) default NULL,
      PRIMARY KEY  (`id`)
    );
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Egypt
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much Ernie1 it is working here at last

    now just to understand what is going on

    the form tag i entered was wrong i should have user the

    enctype="multipart/form-data" instead of "text/plain"

    the rest of the html editing was from observing the missing fields from the database
    and you did nothing to the php script

    Am I correct?

    thanks to you ernie1 and also to cranial-bore for you valuable help in this problem

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have just added the district field to the form and changed the enctype.

    Just to remind you that in php variables are case sesitive.

    Also, don't forget to sanitize the data before inserting into database, just use
    mysql_real_escape_string($var);

    I'm glad you sorted out.
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •