I am trying to create a form that updates mySQL-database.
I've created three php-pages.

updatepizza.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="billie"; // Mysql password 
$db_name="cygna_fyraarstider"; // Database name 
$tbl_name="pizza_menu"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);


?>
<br><br><br>
<center>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="500" border="1" bordercolor="#000000"  cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>Pizza</strong> </td>
</tr>

<tr>
<td align="center"><strong>Titel</strong></td>
<td align="center"><strong>Ingredienser</strong></td>
<td align="center"><strong>Pris</strong></td>
<td align="center"><strong>Uppdatera</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['titel']; ?></td>
<td><? echo $rows['ingrediens']; ?></td>
<td><? echo $rows['pris']; ?></td>

<td align="center"><a href="updatepizza-process.php?id=<? echo $rows['id']; ?>">Uppdatera</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>
</center>
<?php
mysql_close();
?>

updatepizza-process.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="billie"; // Mysql password 
$db_name="cygna_fyraarstider"; // Database name 
$tbl_name="pizza_menu"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);


?>
<br><br><br>
<center>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="500" border="1" bordercolor="#000000"  cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>Pizza</strong> </td>
</tr>

<tr>
<td align="center"><strong>Titel</strong></td>
<td align="center"><strong>Ingredienser</strong></td>
<td align="center"><strong>Pris</strong></td>
<td align="center"><strong>Uppdatera</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['titel']; ?></td>
<td><? echo $rows['ingrediens']; ?></td>
<td><? echo $rows['pris']; ?></td>

<td align="center"><a href="updatepizza-process.php?id=<? echo $rows['id']; ?>">Uppdatera</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>
</center>
<?php
mysql_close();
?>

update-success.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="billie"; // Mysql password 
$db_name="cygna_fyraarstider"; // Database name 
$tbl_name="pizza_menu"; // Table name 



mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$sql="UPDATE pizza_menu SET titel='$titel', ingrediens='$ingrediens', pris='$pris' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='updatepizza.php'>Gå tillbaka</a>";
}

else {
echo "ERROR";
}

?>

It shows "Successful" when I've updated a row but it doesn't change anything in the database. What is the problem? I've been trying to figure it out for days now.

Recommended Answers

All 11 Replies

I am trying to create a form that updates mySQL-database.

Where is the form?

Sorry, the updatepizza-process.php is:

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="billie"; // Mysql password 
$db_name="cygna_fyraarstider"; // Database name 
$tbl_name="pizza_menu"; // Table name 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");



$id=$_GET['id'];


$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update-success.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Uppdatera pizza</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Titel</strong></td>
<td align="center"><strong>Ingredienser</strong></td>
<td align="center"><strong>Pris</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>">
</td>
<td align="center">
<input name="ingrediens" type="text" id="ingrediens" value="<? echo $rows['ingrediens']; ?>" size="15">
</td>
<td>
<input name="pris" type="text" id="pris" value="<? echo $rows['pris']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</center>

<?php
// close connection 
mysql_close();
?>

Your update-success.php page has the statement -

$sql="UPDATE pizza_menu SET titel='$titel', ingrediens='$ingrediens', pris='$pris' WHERE id='$id'";

...but what is the value of $titel, $ingrediens and $pris when the page loads?
at the top of your update-success.php page, put this:

echo "titel = $titel, ingrediens = $ingrediens, pris = $pris"; die();

The value of the variables will be shown on your screen, what are the values?

I am adding 4 lines following line between line 11 and 14 in your update_success.php before update query, you do same

mysql_select_db("$db_name")or die("cannot select DB");

    $titel=$_POST['titel'];
    $ingrediens=$_POST['ingrediens'];
    $pris=$_POST['pris'];
    $id=$_POST['id'];



$sql="UPDATE pizza_menu SET titel='$titel', ingrediens='$ingrediens', pris='$pris' WHERE id='$id'";
$result=mysql_query($sql);

it shows:

titel = , ingrediens = , pris =

Have you read my reply above

@Vingklippt - Exactly, the variables are empty and you are trying to insert little packets of nothingness into your database :D

When you specified method="post" in your form declaration, what does that mean?

It means that the variables you are collecting in your form ($titel, $ingrediens and $pris), will be available to your script on update-success.php (the action of your form declaration) in the 'post' array -
$_POST['titel'], $_POST['ingrediens'] etc...

You could have put method="get" into your form, and then the collected data would be available in the $_GET array.

This is the very basics of PHP/Mysql, you need to be able to do this in your sleep :)

I'm aware of my n00bieness ^^ I'll try that out and see where I get.

@Vingklippt, we were all new once, nothing to be ashamed of :D

@urtrivedi - I'm sure you are just trying to be helpful, but will you also change his nappy after you fed him? At the top of the PHP forum there is a post by the world renown Pritaeas "read this before posting" and one of the points made:

"Do not ask for code. We are not a coding service. We will help you fix your code. If anyone posts a complete working solution for you, they are enabling cheaters. If you use that code you are a cheater."

I know what to post and when to post. I have not written any code for him. He is already doing. I have guided him what he is missing.

Pritaeas means some newbie looking for complete system without any efforts, like they say how to write query, how to develop library system.

Pritaeas expect them to start and put some efforts, on which we can further help newbies.

I love you guys. :D Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.