jdluter Posted January 9, 2008 Share Posted January 9, 2008 Ok to get straight to the point. I have tried multiple ways to create a form that will add data to a mysql table. I know I got the coding right, but it will still not add to the database. It's like its not letting the form insert data to the table. Any help is appreciated thanks **I'm new to php** Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/ Share on other sites More sharing options...
Caesar Posted January 9, 2008 Share Posted January 9, 2008 Please post some of your code. Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434122 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 here is a form im trying to use <html> <body> <?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); if ($submit) { // here if no ID then adding else we're editing if ($id) { $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id"; } else { $sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')"; } // run SQL against the DB $result = mysql_query($sql); echo "Record updated/edited!<p>"; } elseif ($delete) { // delete a record $sql = "DELETE FROM employees WHERE id=$id"; $result = mysql_query($sql); echo "$sql Record deleted!<p>"; } else { // this part happens if we don't press submit if (!$id) { // print the list if there is not editing $result = mysql_query("SELECT * FROM employees",$db); while ($myrow = mysql_fetch_array($result)) { printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]); printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]); } } ?> <P> <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a> <P> <form method="post" action="<?php echo $PHP_SELF?>"> <?php if ($id) { // editing so select a record $sql = "SELECT * FROM employees WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); $id = $myrow["id"]; $first = $myrow["first"]; $last = $myrow["last"]; $address = $myrow["address"]; $position = $myrow["position"]; // print the id for editing ?> <input type=hidden name="id" value="<?php echo $id ?>"> <?php } ?> First name:<input type="Text" name="first" value="<?php echo $first ?>"><br> Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br> Address:<input type="Text" name="address" value="<?php echo $address ?>"><br> Position:<input type="Text" name="position" value="<?php echo $position ?>"><br> <input type="Submit" name="submit" value="Enter information"> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434128 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 where do you get the value of those variable. is your register global on? i suggest try to use POST Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434129 Share on other sites More sharing options...
kenrbnsn Posted January 9, 2008 Share Posted January 9, 2008 You are assuming that register_globals is enabled. It is disabled on most hosts these days because it can create security problems when enabled. Change: <?php if ($submit) { // here if no ID then adding else we're editing if ($id) { $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id"; ?> to <?php if (isset($_POST['submit'])) { // here if no ID then adding else we're editing if ($_POST['id'] != '') { $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id"; ?> There are other changes to be made also. Where ever you reference a plain variable that you're where you're expecting the value to come from the form, you need to get the value from the $_POST superglobal array like I did above. Ken Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434131 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 to be completely honest and truthful, I have no idea what that means exactly. I'll mess around with it as best I can. It now submits the id number to the database, but does not submit the name or other info. If you are willing to advise me more it would be greatly appreciated. I've been wanting to try php for a while because it has many applications. I'm modifying this page to make it into a song request submission page for a web radio station. I apologize for causing trouble. register_globals is on Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434136 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 <a href="http://www.w3schools.com/php/php_post.asp"> read here</a> that should give an idea on how to use post variable Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434143 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 Thanks for that link, I think I understand, but how do i use it more in the code i have? I dont see where it will be used... Im sorry about this Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434151 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 i believe the variables your used are supposed to be post data so try to replace the $first with $_POST['first'] and also the other variables.. Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434159 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 every variable? Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434162 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 ok do this run your page and add this line echo '<pre>'; print_r($_POST); echo '</pre>'; and print here the result from there we will know what to change in your codes Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434163 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 where do i put it in the code because ive tried a few places and it doesnt change anything except add brackets Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434182 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 you use $first which is the name of your textfield right? those textfield name when you passed the value of the form that should be coded like this $_POST['first'] not just $first.. $first will only work if register global is on Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434192 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 what if register global is on? Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434197 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 and also how do i change this line to avoid errors. = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id"; Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434200 Share on other sites More sharing options...
mmarif4u Posted January 9, 2008 Share Posted January 9, 2008 and also how do i change this line to avoid errors. = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id"; Try to enable the mysql error reporting.Then hopefully u will know what the errors are... $result = mysql_query($sql) or die (mysql_error()); Put this line in every query. Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434203 Share on other sites More sharing options...
jdluter Posted January 9, 2008 Author Share Posted January 9, 2008 found a way to do it thank you all Quote Link to comment https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/#findComment-434204 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.