Two questions: 1) Trying to insert data from a HTML form into a database table. My connection to the database works but the insert doesn't. Can you tell me what is wrong with my query/insert code? $link = mysql_connect('localhost', $username, $password); if (!$link){die('Not connected : ' . mysql_error());} $db_selected = mysql_select_db($database, $link); if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());} $query = "INSERT INTO testtbl VALUES('$IPdata')"; mysql_query($query,$link); 2) How do I handle the form (HTML form uses POST) data to get it into the database? I've used: $PostText = trim(file_get_contents('php://input')); which results in $PostText being: userid=John+Smith&country=Canada but then what do I do? Do I have to break it down into individual fields? Many thanks, Michael
Yes. I know my database connect code works because immediately after trying to INSERT, I execute the following code to list the database table rows and that works fine (I already have a few rows in it - don't ask me how I got them in.). By the way, my $IPdata contains John Smith and my database table consists of only one field named UserID. $link = mysql_connect('localhost', $username, $password); if (!$link){die('Not connected : ' . mysql_error());} $db_selected = mysql_select_db($database, $link); if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());} $query = "SELECT UserID FROM testtbl"; $Results = mysql_query($query, $link); Thanks Michael while ($row = mysql_fetch_array($Results, MYSQL_ASSOC)) { print("{$row["UserID"]}<br>"); //echo $row["UserID"]."<br>"; } mysql_close(); print("EOJ<br>");
answer = varchar(15). By the way again: In addition to my Listing code, my DELETE code (copy below) also works well. So my conclusion is that there is something wrong with my INSERT code. I've tried it with 3 kinds of quotes (" ' `) but that doesn't seem to be the answer. $link = mysql_connect('localhost', $username, $password); if (!$link){die('Not connected : ' . mysql_error());} $db_selected = mysql_select_db($database, $link); if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());} $query = "DELETE FROM `testtbl` WHERE `UserID` = '$IPdata'"; mysql_query($query,$link);} Thanks
Thanks guys/gals. For your info, I found an answer to my first question. I don't know why but the code to connect to the DB in my Delete routine works for deleting rows from the DB but it doesn't work for Inserting rows and visa versa. For reference, see code below. But I still don't have an answer to my 2nd question which may have been missed. It was as follows: How do I handle the form (HTML form uses POST) data to get it into the database? I've used: $PostText = trim(file_get_contents('php://input')); which results in $PostText being (e.g.): userid=John+Smith&country=Canada but then what do I do? Do I have to break it down into individual fields? Many thanks, Michael // DELETE DATABASE TABLE DATA ENTRY============================ if($Choice == "del") {$link = mysql_connect('localhost', $username, $password); if (!$link){die('Not connected : ' . mysql_error());} $db_selected = mysql_select_db($database, $link); if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());} $query = "DELETE FROM `testtbl` WHERE `UserID` = '$IPdata'"; mysql_query($query,$link); mysql_close();} // ADD DATABASE TABLE DATA ENTRY=============================== elseif($Choice == "add") {mysql_connect(localhost,$username); mysql_select_db($database) or die ("Unable to select the database."); $query = "INSERT INTO testtbl VALUES('$IPdata')"; mysql_query($query); mysql_close();}
I just tend to define them as in-script variables: $posttext = $_POST[ 'posttext']; $country = $_POST [ 'country']; PHP: For example, if Monkey was entered into the form then $posttext would JUST be Monkey And if the country was Canada then $country would be JUST Canada I assumed the name in the $_POST bit, you would obviously change it to whatever you named it on the form. Then all you have to do is send them to the DB Hope that helps