Page 1 of 2
Update MySQL DB from a form
Posted: Wed Jun 04, 2008 10:24 am
by Cooperman
Hi guys,
I am now trying to update a MySQL DB from a form.
The L_ID is the name of the row in the DB and the name of the value from the <select> tag.
Can you help?
$myrow = ($_POST['L_ID']);
mysql_query("UPDATE login SET level = '2' WHERE L_ID = ". $myrow["L_ID"] .";");
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:01 am
by Cooperman
Have also tried this but this doesn't work either.
mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. ";");
header('Location: admin2.php');
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:21 am
by comando
try this to see if it works..
mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
header('Location: admin2.php');
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:24 am
by Cooperman
No sorry
The database doesn't change to a 2
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:27 am
by comando
did you get any errors ? or it just doesnt do anything?
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:28 am
by Cooperman
doesn't do anything, just goes back to the page admin2.php
mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
header('Location: admin2.php');
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:31 am
by comando
show me your html post form. Maybe you get the value from the form wrong and thats why the query doesnt work. Do you put all the values from the form into an array called "L_ID"
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:36 am
by Cooperman
HTML Page but the <select> tag is populated by the DB. L_ID is the name of one of the rows in the DB
admin2.php
$sql = ("SELECT firstName FROM login order by firstName");
$result = mysql_query($sql, $link);
?>
<form name="level" method="post" action="levelSQL.php">
<select name="L_ID">
<?
while($myrow = mysql_fetch_array($result)) {
echo "<option value=" . $myrow["L_ID"] . ">" . $myrow["firstName"]. "</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td width="127"> </td>
</tr>
<tr>
<td><input name="submit2" type="submit" value="Upload Level"></td>
</tr>
</form>
levelSQL.php
mysql_query("UPDATE login SET access = 2 WHERE L_ID = " .$_POST['L_ID']. "\"");
header('Location: admin2.php');
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:41 am
by Cooperman
OK, I noticed once I paste the code in here that I hadn't added L_ID into the Select firstName from... code.
This then changed the 1 to a 2 but then when I selected another name, it didn't change it

Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:45 am
by comando
try to print the an array $_POST to see if you can get any value..
print_r($_POST);
put this line in the file levelSQL.php
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:49 am
by Cooperman
No error
mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
print_r($_POST);
header('Location: admin2.php');
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:51 am
by comando
you mean you don't get any value from the form?
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:53 am
by comando
Sorry, just copy the code below and paste into your file levelSQL.php
mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
print_r($_POST);
//header('Location: admin2.php');
because you didn't put 2 slashes infront of header before, thats why it redirect you to admin2 page

Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 11:59 am
by Cooperman
Array ( [L_ID] => 32 [submit2] => Upload Level )
whatever that means lol
Re: Update MySQL DB from a form
Posted: Wed Jun 04, 2008 12:02 pm
by comando
yep

, lets try this then..
$myrow = $_POST['L_ID'];
mysql_query("UPDATE login SET level = 2 WHERE L_ID = \"$myrow\" ");