Update MySQL DB from a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Update MySQL DB from a form

Post 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"] .";");
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post 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');
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post by comando »

try this to see if it works..


mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
header('Location: admin2.php');
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post by Cooperman »

No sorry :(

The database doesn't change to a 2
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post by comando »

did you get any errors ? or it just doesnt do anything?
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post 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');
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post 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"
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post 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">&nbsp;</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');
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post 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 :(
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post 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
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post by Cooperman »

No error :(


mysql_query("UPDATE login SET access = 2 WHERE firstName = " .$_POST['firstName']. "\"");
print_r($_POST);
header('Location: admin2.php');
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post by comando »

you mean you don't get any value from the form?
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post 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 :)
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: Update MySQL DB from a form

Post by Cooperman »

Array ( [L_ID] => 32 [submit2] => Upload Level )

whatever that means lol
comando
Forum Newbie
Posts: 11
Joined: Wed Jun 04, 2008 11:18 am

Re: Update MySQL DB from a form

Post by comando »

yep :), lets try this then..

$myrow = $_POST['L_ID'];
mysql_query("UPDATE login SET level = 2 WHERE L_ID = \"$myrow\" ");
Post Reply