Reputation: 7
I have come across a problem when inserting values from a html select in to a mysql database. I can't seem to get the values to insert for some reason; I have looked for help on this but they keep giving me errors. Also, can some please tell me what the difference between mysql and mysqli?
php code
<?php
$con = mysql_connect("localhost","barsne","bit me");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("testing", $con);
$sql="INSERT INTO client_details (id, f_name, l_name, phone, email, job_est) VALUES
('', '$_POST[f_name]', '$_POST[l_name]', '$_POST[phone]', '$_POST[email]', '$_POST[job_est]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "Thank you for booking a with us we will contact you in the next 24 hours to confirm your booking with a time and date";
mysql_close($con)
?>
html code
<form method="post" action="processing_booking.php">
<h4><u>Basic Contact Details</u></h4>
<label>First Name</label>
<input type="text" name="f_name">
<label>Last Name:</label>
<input type="text" name="l_name" id="l_name">
<label>Phone Number:</label>
<input type="text" name="phone" id="phone">
<label>Email:</label>
<input type="text" name="email" id="email">
<h4><u>Job Details</u></h4>
<label>You Would Like To Book A:</label>
<select name="job_est">
<option value="select">--SELECT--</options>
<option value="job">Job</option>
<option value="est">Estimation</option>
</select>
<label>Service Your Booking:</label>
<select name="job_type">
<option value="select">--SELECT--</option>
<option value="gardening">Gardening</option>
<option value="landscaping">Landscaping</option>
<option value="painting">Painting & Decorating</option>
<option value="decking">Decking & Fencing</option>
</select>
<label>Any Additional Information </label>
<textarea name="extra_info"></textarea>
<input type="submit" value="lets get your dreams started">
</form>
sorry wirting is not my strong point
Upvotes: 0
Views: 10629
Reputation: 1485
Note that $_POST['name']
you missed single quotations
you must prevent from injection with
// String Type Fields
$name = strip_tags($_POST['name']);
$f_name= strip_tags($_POST['f_name']);
$l_name= strip_tags($_POST['l_name']);
$phone= strip_tags($_POST['phone']);
$email= strip_tags($_POST['email']);
// Int Type Fields
if (isset($_POST['job_est']) && is_numeric($_POST['job_est']))
$job_est= $_POST['job_est'];
else
$job_est= 0;
then use in your query
other point is if your id field is primary and auto increment , you can define your query as below :
$sql="INSERT INTO client_details (f_name, l_name, phone, email, job_est) VALUES
( '".$f_name."', '".$l_name."', '".$phone."', '".$email."', ".$job_est.")";
for strings you must use '".$variable."'
and for integer or numeric you must use ".$variable."
other point is you must change your select element to below because you have noticed in your comments your job_est
field type is int(11)
<select name="job_est">
<option value="0">--SELECT--</option>
<option value="1">Gardening</option>
<option value="2">Landscaping</option>
<option value="3">Painting & Decorating</option>
<option value="4">Decking & Fencing</option>
</select>
Upvotes: 2
Reputation: 797
The mysql functions are deprecated. Use the mysqli or pdo class instead.
Make a var_dump($_POST)
and you will see what the problem is. By the way you missed the single quotes $_POST['value']
Never ever write $_POST or $_GET data directly in your sql queries. Always validate them before, because even the value of a selectbox can get easily changed with tampadata or chrome developer tools.
Upvotes: 1
Reputation: 302
Well you can obviously start printing the results of your $_POST array with :
print_r($_POST,1);
to check all variables existence in it
Upvotes: 0