Jump to content


Photo

Updating values in MYSQL


  • Please log in to reply
5 replies to this topic

#1 jgmiddel

jgmiddel
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 January 2006 - 11:10 AM

With this script I (hopefully) created a start for making a form to update the information in my database. What i'm trying is to make a form wich can update all the values.

<?php
include "connect.php";
$query = "SELECT * FROM `prices`";
$sql = mysql_query($query) or die ( mysql_error( ) );
$aantal = mysql_num_rows($sql);
echo"<table cellspacing=\"3\" width=\"100%\" border=\"0\">";
while($record = mysql_fetch_object($sql)){
echo"<tr><td>".$record->description."</td><td>€&nbsp;<input type=\"text\" class=\"formfields\" name=\"".$record->id."\" size=\"6\" value=\"".$record->price."\"></td></tr>";
}
echo"</table><p><input type=\"submit\" value=\"Send\" name=\"B1\" class=\"formfields\"></form>";
?>
<p>

The next step is to post the values to the database: it is a new php-file:

<?
while($record = mysql_fetch_object($sql)){
echo '<input type="text" name="price[]" size="6" value="'.$record->price.'">';
echo '<input type="hidden" name="id[]" value="'.$record->id.'">';
}
echo "<pre>";
print_r($_POST);
foreach ($_POST[prijs] as $key=>$value)
{
echo ($_POST[id[$key]] - $value <br>);
}
?>

Someone helped me with the last part, but there is an error on the last line. Who can help me (just a beginner)


#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 13 January 2006 - 12:30 PM

First, helping is always easier if you provide us with the actual error message, not just say that "there's an error"

Second, your code looks pretty confusing. You are talking about updating, but it's nowhere to be seen. Your 'new php file' has some form elements with no clue etc...

Try this, and maybe things will open up a bit
foreach ($_POST as $key=>$value)
{
echo "$key - $value <br>";
}


#3 jgmiddel

jgmiddel
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 January 2006 - 01:03 PM

Hi,

Thanks for your respons. I changed my code, but I don't know how to make a query for updating the database.

This is the page with the form. It's OK, I guess.

<?php
include "connect.php";
$query = "SELECT * FROM `prijzen`";
$sql = mysql_query($query) or die ( mysql_error( ) );
echo"<form method=\"POST\" action=\"test3.php\">" ;
echo"<table cellspacing=\"3\" width=\"100%\" border=\"0\">";
while($record = mysql_fetch_object($sql)){
echo"<tr><td>".$record->omschrijving."</td><td>€&nbsp;<input type=\"text\" class=\"formfields\" name=\"".$record->id."\" size=\"6\" value=\"".$record->prijs."\"></td></tr>";
}
echo"</table><p><input type=\"submit\" value=\"Verzenden\" name=\"B1\" class=\"formfields\"></form>";
?>

In another file there must be query for updating the database and the new values must be confirmed on the screen:

<?
while($record = mysql_fetch_object($sql)){
echo '<input type="text" name="prijs[]" size="6" value="'.$record->prijs.'">';
echo '<input type="hidden" name="id[]" value="'.$record->id.'">';
}
echo "<pre>";
print_r($_POST);
foreach ($_POST[prijs] as $key=>$value)
{
echo "$_POST[id][$key] - $value <br>";
}
?>

If is better to do it all in one file, please let me know.

Thanks for helping, I really do not know how to get further.



#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 January 2006 - 07:47 PM

As LazyJones indicated, there is no INSERT/UPDATE statement in your code. Since you have all the name/value pairs, you can easily construct such a statement.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 jgmiddel

jgmiddel
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 January 2006 - 07:58 PM

"you can easily..." I'm a beginner and really do not know how to write a query wich the wanted result. Any help would be appriciated.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 January 2006 - 08:12 PM

You need an UPDATE query like the following:

UPDATE `prijzen` SET field1='value1', field2='value2', ..... WHERE id = 'record_id';

Since you have all of the fields in the $_POST hash, you should have no problem constucting this query. If you can't figure it out, there are literally hundreds of tutorials out there. Good luck.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users