i used this code to insert data into database but nothing happening..

<?php
    session_start();

    if(isset($_POST['submit']))
    {
        $dbhost="localhost";
        $dbuser="root";
        $dbpass="";
        $tablename="requested_quotes";
        $conn=mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect");
        mysql_select_db('1')or die("cannort select DB");
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $sex=$_POST['sex'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $address1=$_POST['address1'];
        $address2=$_POST['address2'];
        $country=$_POST['country'];
        $city=$_POST['city'];
        $state=$_POST['zip'];
        $date=$_POST['date'];
        $contactme=$_POST['contactme'];
        $interest=$_POST['interest'];

    $sql="INSERT INTO $tablename(first_name, last_name, sex, email,phone, address1, address2,country, city,zip, respond_date, contact_via,interest)
    values('$fname','$lname', $sex,'$mail',$email, $phone,$address1, $address2, $country, $city, $state, $date, $contactme, $interest)";
    $result=mysql_query($sql);

        if($result)
        {
         $session['message'] = "successfully inserted into datebase";
        }
        else
        {
         $session['message'] = "failed to insert into datebase"; 
        }

    }

?>


<form name="signup" action="" method="POST" onsubmit="return validate()"><br/>
    First Name:<input type="text" placeholder="First Name" name="fname"><br/>
    Last Name:<input type="text" placeholder="Last Name" name="lname"><br/>
    gender:</select>
    <input type="radio" name="sex"  value="male" checked="checked">Male
    <input type="radio" name="sex"  value="female">Female<br/>
    Email:<input type="text" placeholder="Your Email" maxlength="30" size="45" name="email">  <br /> 
    Phone:<input type="text" placeholder="Contact number" name="phone"><br/> 
    Address1:<input type="text" placeholder="address1" name="address1"><br/>
    Address2:<input type="text" placeholder="address2" name="address2"><br/>  
    Country:<select name="country" size="">
        <option value="">select..</option>
        <option value="uk">UK</option>
        <option value="usa">USA</option>
        <option value="spain">Spain</option>
        <option value="france">France</option>
        <option value="china">China</option>
        <option value="japan">Japan</option>
    </select><br />
    City:<input type="text" placeholder="city" name="city"><br/>
    State:<input type="text" placeholder="State" name="state"><br/> 
    Zip Code:<input type="text" placeholder="zip code" name="zipcode"><br/> 
    Date Respond required:<input type="text" placeholder="d/m/y " name="date"><br/>
    Contact me via:<select name="contactme" size="">
        <option value="email">Email</option>
        <option value="phone">Phone</option>
        <option value="post">Post</option>
        </select><br />
    servides Interested:</select>
    <input type="checkbox" name="interest[]"  value="web">Web
    <input type="checkbox" name="interest[]" value="database">Database
    <input type="checkbox" name="interest[]" value="networkings">Networking
    </select><br />
    <input type="submit" name="submit" value="Submit" >
</form>

...and my database is as follows

Recommended Answers

All 7 Replies

$sql="INSERT INTO

$tablename

(first_name, last_name, sex, email,phone, address1, address2,country, city,zip, respond_date, contact_via,interest)
values('$fname','$lname', $sex,'$mail',$email, $phone,$address1, $address2, $country, $city, $state, $date, $contactme, $interest)";

What is your

tablename

?

$tablename="requested_quotes";

your table name is requested_quotes correct?
so ur sql is like this:

$sql="INSERT INTO requested_quotes(first_name, last_name, sex, email,phone, address1, address2,country, city,zip, respond_date, contact_via,interest)
    values('$fname','$lname', $sex,'$mail',$email, $phone,$address1, $address2, $country, $city, $state, $date, $contactme, $interest)";

change $tablename to the actual table name :requested_quotes

Member Avatar for diafol

Could be a number of things.

Do you get any errors? You do not provide any die().

Try the form without the onsubmit. You don't include the javascript function, so it's difficult to see if this is actually being submitted or just stops after validation. I would have thought that this is OK though if you get page reload.

WRT $tablename - that should be OK since the $tablename var is set.

Your fieldlist:

(first_name, last_name, sex, email,phone, address1, address2,country, city,zip, respond_date, contact_via,interest)

does not match the screenshot, because the 'address2' field is 'addrss2' in the screenshot.

There may be other issues - I've only taken a cursory look.

P.S.

This is very unsafe. You do not provide any server-side validation and your SQL statement leaves you wide open to SQL injection. DO NOT use this on a production site. Use mysqli or PDO and parameterized queries.

the problem is that you insert $interest directement, but, $interest is a array. you can insert array directly in insert request.
try somthing like $interest = implode(',', $_POST['interest']); before intering it

Member Avatar for diafol

^^Good spot.

Unfortunately, storing interest as a string will make it difficult to edit the record at a later date or to search without resorting to LIKE.

One option would be to use something like:

<input type="checkbox" name="interest[]" value="1">Web
<input type="checkbox" name="interest[]" value="2">Database
<input type="checkbox" name="interest[]" value="4">Networking

Then

$interest = (isset($_POST['interest'])) ? array_sum($_POST['interest']) ? 0;

This will give totals like:

0 - no checkboxes
1 - web only
2 - database only
3 - web and database
4 - networking only
5- web and networking
6 - database and networking
7 - all options

You then just store the total (an integer between 0 and 7)

In order to retrieve all records that have database in them (2), you'd do this...

SELECT ... FROM table WHERE interest & 2

Likewise in php edit form...

<input type="checkbox" name="interest[]" value="1"<?php if($row['interest] & 1) echo ' "checked"';?>>Web
<input type="checkbox" name="interest[]" value="2"<?php if($row['interest] & 2) echo ' "checked"';?>>Database
<input type="checkbox" name="interest[]" value="4"<?php if($row['interest] & 4) echo ' "checked"';?>>Networking

You can have up to 32 linked checkboxes (32-bit systems) like this.

thank you for all...finally i've solved my problem. this code may be helpful for beginner php developers like me...so here's the code..

<?php

    if(isset($_POST['submit']))
    {
        $dbhost="localhost";
        $dbuser="root";
        $dbpass="";
        $conn=mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect");
        mysql_select_db('1')or die("cannot select DB");

        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $sex=$_POST['sex'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $address1=$_POST['address1'];
        $address2=$_POST['address2'];
        $country=$_POST['country'];
        $city=$_POST['city'];
        $zip=$_POST['zip'];
        $state=$_POST['state'];
        $date=$_POST['date'];
        $contact=$_POST['contact_me'];

        $interest = implode(',', $_POST['interest']);

        $sql="INSERT INTO requested_quotes(first_name, last_name, sex, email,phone, address1, address2,country, city,zip, respond_date, contact_via,interest)
        values('$fname','$lname', '$sex', '$email', '$phone','$address1', '$address2', '$country', '$city', '$zip', '$date', '$contact', '$interest')";
        $result=mysql_query($sql);

        if($result)
        {
             echo "successfull";
        }
        else
        {
             echo "failed";
        }
    }
?>


<form name="signup" action="" method="POST" enctype="multipart/form-data"><br/>
    First Name:<input type="text" placeholder="First Name" name="fname"><br/>
    Last Name:<input type="text" placeholder="Last Name" name="lname"><br/>
    gender:</select>
    <input type="radio" name="sex"  value="male" checked="checked">Male
    <input type="radio" name="sex"  value="female">Female<br/>

    Email:<input type="text" placeholder="Your Email" maxlength="30" size="45" name="email">  <br /> 
    Phone:<input type="text" placeholder="Contact number" name="phone"><br/> 
    Address1:<input type="text" placeholder="address1" name="address1"><br/>
    Address2:<input type="text" placeholder="address2" name="address2"><br/>  
    Country:
    <select name="country" size="">
        <option value="">select..</option>
        <option value="uk">UK</option>
        <option value="usa">USA</option>
        <option value="spain">Spain</option>
        <option value="france">France</option>
        <option value="china">China</option>
        <option value="japan">Japan</option>
    </select><br />

    City:<input type="text" placeholder="city" name="city"><br/>
    State:<input type="text" placeholder="State" name="state"><br/> 
    Zip Code:<input type="text" placeholder="zip code" name="zip"><br/> 
    Date Respond required:<input type="text" placeholder="d/m/y " name="date"><br/>
    Contact me via:
    <select name="contact_me" size="">
        <option value="">select..</option>
        <option value="email">Email</option>
        <option value="phone">Phone</option>
        <option value="post">Post</option>
    </select><br />

    servides Interested:
    </select>
        <input type="checkbox" name="interest[]"  value="web">Web
        <input type="checkbox" name="interest[]" value="database">Database
        <input type="checkbox" name="interest[]" value="networking">Networking
    </select><br />
    <input type="submit" name="submit" value="Submit" >
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.