...

View Full Version : Form updates table row



bucket
11-04-2009, 06:27 PM
I would like to make this script so that instead of inserting info it updates the code to update the first row.

I thought of adding this to the update

WHERE id='$id'and having this to say what row

$id = $rows['id'];So something like this:



<?php
require('inc/config.php');
$id = $rows['id'];
$sql="UPDATE Persons WHERE id='$id' (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?> And the form:


<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html> Is that correct?

and mostly will it work?

bucket
11-04-2009, 08:06 PM
Okay I currently got this for the form called page1.php


<html>
<body>

<form action="upage2.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
middlename: <input type="text" name="middlename" />
<input type="submit" />
</form>

</body>
</html>

and this for the page2.php


<?php
include ('inc/config.php');
$id = $rows['id'];

mysql_query("
UPDATE Persons SET
$FirstName = 'mysql_real_escape_string ($_POST[firstname])',
$LastName = 'mysql_real_escape_string ($_POST[lastname])',
$MiddleName= 'mysql_real_escape_string ($_POST[middlename])';
WHERE id='$id'
");

?>
It isnt working at all and I am getting no errors

djm0219
11-04-2009, 08:37 PM
In page2.php where is $rows['id'] coming from?

bucket
11-04-2009, 09:00 PM
I fixed that now there is a new error:


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'LastName', 'MiddleName'' at line 1



<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once ('inc/config.php');
$query = "SELECT * FROM testing WHERE FirstName, LastName, MiddleName";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result))
{
echo $row['FirstName'];
echo $row['LastName'];
echo $row['MiddleName'];
}
?>

met
11-04-2009, 09:50 PM
learn SQL first, read what you are telling the query to do..it makes no sense.



SELECT what you want
FROM where it's stored
WHERE some condition is met


at the moment you are telling SQL to SELECT all FROM testing WHERE FirstName, LastName, MiddleName...that makes no sense at all.

either assign conditions:

WHERE FirstName='Fred'

etc

or



$q = 'SELECT FirstName, LastName, MiddleName FROM testing';

// is probably what you want..

bucket
11-04-2009, 10:39 PM
I have fixed that problem.

Now i got a question...


I would like to echo it separately in different places of my webpage, can I just do this?

At the top of the page

<?php
require_once ('inc/config.php');
$query = "SELECT * FROM testing";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result))
?>


Around the webpage.



<?php $row['MiddleName'] ?>

or

<link rel="stylesheet" type="text/css" href="<?php $row['Url'] ?>/css/style.css" />

or

<title><?php $row['WebsiteTitle'] ?></title>



Like that? Or I have to have it echo?

met
11-04-2009, 10:44 PM
a while loop in this context will perform the code for each record in the database.

if you have ten records, the code between the while { } is done 10 times.

if you have just one record, you can do



$row=mysql_fetch_array($query);

$row['x']; // now contains an array of data from the db, so yes, you could do:

<title><?php echo $row['WebsiteTitle']; ?> </title>

but if you do this in a WHILE loop then you will end up with X amount of data printed out.

what are you trying to achieve exactly, there is definitely some confusion

bucket
11-04-2009, 10:45 PM
errrh... What??

What does that mean?

met
11-04-2009, 10:54 PM
<?php
require_once ('inc/config.php');
$query = "SELECT * FROM testing";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result)) { // do something
}

/* ok so this code gets all the data from the table testing, and
then you pass the result into a while loop. in the while loop,
you are setting an array $row with the values from the query.

this data can be accessed like so: */

$foo = $row['columnName'];
echo $row['someValue'];

/* a WHILE loop is used to repeat the code between the bracers for every record returned.
this is useful when you need to display sets of data, e.g. a list of members

to access this data you need to do something with it during the while loop.

*/
?>


you are being too vague to get any real help. What is stored in table testing? what are you trying to achieve...

bucket
11-04-2009, 11:10 PM
<?php
require_once ('inc/config.php');
$query = "SELECT * FROM testing";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result)) {
?>

TextHere: <br>
<?php $foo = $row['FirstName'];?>

<br>
Text here <br>
<?php echo $row['MiddleName'];?>

<?php
}
?>



Will that work?

met
11-05-2009, 09:21 AM
technically yes

the code i supplied was an example

$foo = $row['FirstName']; will just put the value of FirstName in to the variable $foo and over write it each time, accomplishing nothing.

the second will echo out the value of MiddleName for each record in the database.

again if you could be more specific about WHAT you want to do i can help more



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum