Jump to content

unable to input value into database table using php code


genzedu777

Recommended Posts

Hi,

 

I need some help here, the one which I have highlighted in red, 'dob' and 'gender', are not inputting any values into my database table. I'm just wondering did I miss out something important, or should I change the 'type' in my database table? Thanks

 

 

 

<?php

 

$dob = $_POST['dob'];

$gender = $_POST['gender'];

 

/**INSERT into tutor_profile table**/

$query2 = "INSERT INTO tutor_profile (name, nric, dob, gender, race) VALUES ('$name', '$nric', '$dob', '$gender', '$race')";

 

$results2 = mysqli_query($dbc, $query2)

or die(mysqli_error());

 

?>

 

<html>

<div>

<label for="dob" class="label">Date Of Birth</label>

<input name="dob" type="text" id="dob"/>

</div>

</html>

 

<!--Race-->

<div>

<label for="race" class="label">Race</label>

 

<?php

echo '<select name="race" id="race">

<option value="">--Please select one--</option>';

 

$dbc = mysqli_connect('localhost', '111', '111', '111')

or die(mysqli_error());

$query = ("SELECT * FROM race ORDER BY race_id ASC");

$sql = mysqli_query($dbc, $query)

or die(mysqli_error());

 

while($data = mysqli_fetch_array($sql)) {

echo'<option value="'.$data['race_id'].'">'.$data['race_name'].'</option>';

}

echo '</select><br/>';

 

mysqli_close($dbc); ?>

 

</div>

 

[attachment deleted by admin]

Link to comment
Share on other sites

 

/**INSERT into tutor_profile table**/

$query2 = "INSERT INTO tutor_profile (name, nric, dob, gender, race) VALUES ('$name', '$nric', '$dob', '$gender', '$race')";

 

$results2 = mysqli_query($dbc, $query2)

or die(mysqli_error());

 

You should be getting an error because there is no column 'race' in the table you provided only 'race_id'.  The insert shouldn't work at all at this point.  Why do you having a closing html tag after the dob input?

Link to comment
Share on other sites

Hi ninedoors,

 

Thanks, I got it. Because in my SQL database, I have stated it as 'race_id', as I insert, I should INSERT into race_id, not race.

 

However I have another question here. But why is it that my 'dob' column in my SQL table is empty, even though all variables' name have been set correctly. Could you advise on this area. Thank you

 

 

<?php

 

$dob = $_POST['dob'];

 

$query = "INSERT INTO tutor_profile (dob) VALUES ('$dob')";

 

$results = mysqli_query($dbc, $query)

or die(mysqli_error());

 

?>

 

 

<div>

        <label for="dob" class="label">Date Of Birth</label>

        <input name="dob" type="text" id="dob"/>

</div>

 

Link to comment
Share on other sites

Like PFMaBiSmAd said, you probably don't have an input name="dob" in your form, since it's not even getting to the query string.

 

Also, please, please, please, sanitize your variables before you put them into the database! A simple

$dob = mysql_real_escape_string($_POST['dob']);
$gender = mysql_real_escape_string($_POST['gender']);

will help immensely.

Link to comment
Share on other sites

Any idea what happen?

 

Yes, mdewyer posted a generic answer without reading your code to see that you are using mysqli_ functions and since you have not bothered to read the php.net documentation for the functions you are putting into your code, you don't know the proper parameters to put into the function call, even after you got an error message that indicates the wrong number of parameters.

 

Programming language reference documents exist for a reason, so that you can look up how to use the elements of those programming languages. Programming languages are one of the more heavily documented subjects on the planet because it is simply impossible to effectively use a programming language without reading the documentation for whatever elements you are using of that language.

Link to comment
Share on other sites

My bad. Sorry I missed the "i" there. I was just trying to convey the importance of sanitizing user-submitted values and gave the example I'm most familiar with, totally missing that mysqli was being used. Hopefully that points you in the right direction though. (Like PFMaBiSmAd said, the documentation for those functions will be very helpful.)

Link to comment
Share on other sites

Hey guys,

 

I've made a typo error, PFMaBiSmAd i did change it to the below code, and it didn't work, because if you have seen my error message stated below, you would have known that I have changed it mysqli. So, it still does not work, any idea? Thanks

 

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in D:\inetpub\vhosts\111.com\httpdocs\111.php on line 210

 

$dob = mysqli_real_escape_string($_POST['dob']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.