genzedu777 Posted November 8, 2010 Share Posted November 8, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/ Share on other sites More sharing options...
ninedoors Posted November 8, 2010 Share Posted November 8, 2010 /**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? Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1131795 Share on other sites More sharing options...
genzedu777 Posted November 8, 2010 Author Share Posted November 8, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1131803 Share on other sites More sharing options...
ninedoors Posted November 8, 2010 Share Posted November 8, 2010 $query = "INSERT INTO tutor_profile (dob) VALUES ('$dob')"; After this can you echo $query so you know that $dob is not empty? Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1131811 Share on other sites More sharing options...
genzedu777 Posted November 9, 2010 Author Share Posted November 9, 2010 Hi ninedoors, I found out it is empty, no values has been inputted. Do you have any idea what caused the problem? Thanks INSERT INTO tutor_profile (dob) VALUES ('') Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132087 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2010 Share Posted November 9, 2010 It's likely that your form is invalid (the dob field is not located inside the form.) Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132089 Share on other sites More sharing options...
mdewyer Posted November 9, 2010 Share Posted November 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132105 Share on other sites More sharing options...
genzedu777 Posted November 9, 2010 Author Share Posted November 9, 2010 Hi, By putting $dob = mysql_real_escape_string($_POST['dob']); I have received an error message... Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in D:\inetpub\vhosts\111.com\httpdocs\111.php on line 210 Any idea what happen? Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132216 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2010 Share Posted November 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132234 Share on other sites More sharing options...
mdewyer Posted November 9, 2010 Share Posted November 9, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132272 Share on other sites More sharing options...
genzedu777 Posted November 10, 2010 Author Share Posted November 10, 2010 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']); Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132490 Share on other sites More sharing options...
mdewyer Posted November 10, 2010 Share Posted November 10, 2010 According to the PHP documentation for mysqli_real_escape_string (http://php.net/manual/en/mysqli.real-escape-string.php), you need to pass the link/resource identifier as the first parameter of the function. So, give this a shot: $dob = mysqli_real_escape_string($dbc, $_POST['dob']); Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1132493 Share on other sites More sharing options...
genzedu777 Posted November 13, 2010 Author Share Posted November 13, 2010 thanks Quote Link to comment https://forums.phpfreaks.com/topic/218104-unable-to-input-value-into-database-table-using-php-code/#findComment-1133772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.