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

Dani AI

Generated

Nice catches by @diafol (mismatched field names) and (checkbox array). Two more things to future‑proof this for anyone landing here today: the old mysql_* API used in the original snippet will not run on PHP 7+ (removed after being deprecated in 5.5). Use mysqli or, preferably, PDO with prepared statements to avoid SQL injection and to get real error messages. See the PHP manual for details: mysql extension status and PDO prepared statements.

If you want to store multiple checkbox choices without making later queries painful, use a junction table instead of a comma‑separated string. Example with PDO and a pivot table requested_quote_interest (quote_id, interest_slug). It also handles the case where no checkboxes are selected and wraps the whole thing in a transaction:

<?php
$pdo = new PDO(
    'mysql:host=localhost;dbname=yourdb;charset=utf8mb4',
    'user', 'pass',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$interests = isset($_POST['interest']) && is_array($_POST['interest']) ? $_POST['interest'] : [];

$pdo->beginTransaction();

$stmt = $pdo->prepare(
 'INSERT INTO requested_quotes
  (first_name,last_name,sex,email,phone,address1,address2,country,city,zip,respond_date,contact_via)
  VALUES (:first_name,:last_name,:sex,:email,:phone,:address1,:address2,:country,:city,:zip,:respond_date,:contact_via)'
);

$stmt->execute([
 'first_name'=>$_POST['fname'] ?? '',
 'last_name'=>$_POST['lname'] ?? '',
 'sex'=>$_POST['sex'] ?? null,
 'email'=>$_POST['email'] ?? '',
 'phone'=>$_POST['phone'] ?? '',
 'address1'=>$_POST['address1'] ?? '',
 'address2'=>$_POST['address2'] ?? '',
 'country'=>$_POST['country'] ?? '',
 'city'=>$_POST['city'] ?? '',
 'zip'=>$_POST['zip'] ?? '',
 'respond_date'=>$_POST['date'] ?? null,
 'contact_via'=>$_POST['contactme'] ?? null,
]);

$quoteId = (int)$pdo->lastInsertId();
$qi = $pdo->prepare('INSERT INTO requested_quote_interest (quote_id, interest_slug) VALUES (:qid, :slug)');
foreach ($interests as $slug) {
    $qi->execute([':qid'=>$quoteId, ':slug'=>$slug]);
}

$pdo->commit();

Quick debugging tips if nothing inserts: temporarily remove client‑side onsubmit validation; enable exceptions as above; verify name= attributes exactly match your column list; and if you only need text fields, drop enctype="multipart/form-data" (it is for file uploads). If you truly must keep a single column for interests, consider a JSON column on MySQL 5.7+ so you can query it sensibly later: MySQL JSON type.

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 Member #120589

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 Member #120589

^^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.