 |
|
|
11-04-2009, 02:25 PM
|
Change insert to update
|
Posts: 115
Name: Not Telling
|
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
and having this to say what row
PHP Code:
$id = $rows['id'];
So something like this:
PHP Code:
<?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:
PHP Code:
<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?
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
Last edited by sith717; 11-04-2009 at 02:27 PM..
|
|
|
|
11-04-2009, 02:33 PM
|
Re: Change insert to update
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
No.
Update uses the
Code:
UPDATE table SET columnA = valueA, SET columnB = valueB WHERE criteria = value;
syntax.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-04-2009, 02:37 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
So how would the updated code look like?
I really dont understand.
Like edited so it will work with my form.
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
Last edited by sith717; 11-04-2009 at 02:40 PM..
|
|
|
|
11-04-2009, 02:50 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
So something like this?
PHP Code:
<?php include ('inc/config.php'); $id = $rows['id'];
mysql_query("UPDATE Persons WHERE id='$id' SET Age = '$_POST[age]' WHERE FirstName = '$_POST[firstname]' AND LastName = '$_POST[lastname]'");
?>
Something like that?
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
Last edited by sith717; 11-04-2009 at 02:51 PM..
|
|
|
|
11-04-2009, 03:00 PM
|
Re: Change insert to update
|
Posts: 335
Name: Jerry
|
you don't need to do that since your id is unique all you need is
PHP Code:
mysql_query("UPDATE Persons SET Age = '$_POST[age]' WHERE id='$id'");
as you said in your last thread about sanitizing input you should keep to that yourself... personally I would not send inputs straight to my db with out verifying the data... in this case you should
PHP Code:
if (is_numeric($_POST['aga'])) mysql_query("UPDATE Persons SET Age = '$_POST[age]' WHERE id='$id')"); else echo 'Please enter valid age';
|
|
|
|
11-04-2009, 03:04 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
The age was an example.
So ye. :P
I will have no ages in it.
So it will be like this?
PHP Code:
<?php include ('inc/config.php'); $id = $rows['id'];
mysql_query("UPDATE Persons WHERE id='$id' SET FirstName = '$_POST[firstname]', LastName = '$_POST[lastname]', MiddleName = '$_POST[middlename]' ");
?>
Like that? Saying that middle name is a textbox in the form.
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
11-04-2009, 03:12 PM
|
Re: Change insert to update
|
Posts: 80
Name: John
Location: Sacramento
|
Better: "...firstname = mysql_real_escape_string(trim(".$_POST['firstname']."))..."
|
|
|
|
11-04-2009, 03:16 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
Okay I have done that, will it work?
PHP Code:
<?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' "); ?>
Is that correct? and will it work?
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
Last edited by sith717; 11-04-2009 at 03:17 PM..
|
|
|
|
11-04-2009, 03:26 PM
|
Re: Change insert to update
|
Posts: 335
Name: Jerry
|
that should work no problem but if it doesn't it maybe the placement of quotations
PHP Code:
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' ");
|
|
|
|
11-04-2009, 03:44 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
Okay I currently got this for the form called page1.php
PHP Code:
<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 Code:
<?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
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
11-04-2009, 03:50 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
I have also tried it like this
PHP Code:
<?php include ('inc/config.php'); $id = $rows['id'];
$sql = mysql_query (" UPDATE `persons` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."' WHERE `id` = '".$id."' ");
?>
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
11-04-2009, 03:52 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
Im getting these errors:
Quote:
Notice: Undefined variable: rows in /home/runehost/public_html/update.php on line 7
Notice: Undefined index: firstname in /home/runehost/public_html/update.php on line 13
Notice: Undefined index: lastname in /home/runehost/public_html/update.php on line 14
Notice: Undefined index: middlename in /home/runehost/public_html/update.php on line 15
|
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
11-04-2009, 04:35 PM
|
Re: Change insert to update
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
PHP Code:
$id = $rows['id'];
What is $rows? You need to have the id of the row that you want to update.
Then do this.
PHP Code:
$first = mysql_real_escape_string($_POST['first']); $last = mysql_real_escape_string($_POST['last']); $middle = mysql_real_escape_string($_POST['middle']);
// I split the line so it fits in the forum code box $query = "UPDATE persons SET first='$first', last='$last', middle='$middle' WHERE id='$id'"; mysql_query($query) or die(mysql_error());
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
Last edited by lizciz; 11-04-2009 at 04:38 PM..
|
|
|
|
11-04-2009, 04:42 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
PHP Code:
<?php include ('inc/config.php'); $firstname = mysql_real_escape_string ($_POST['firstname']); $lastname = mysql_real_escape_string ($_POST['lastname']); $middlename= mysql_real_escape_string ($_POST['middlename']);
$id = 2; $sql = mysql_query (" UPDATE `testing` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `MiddleName` = '".$middlename."' WHERE `id` = '".$id."' ") OR die (mysql_error()); ?>
Fixed so there's no error
Right now it is editing row 2. But in my column I only have 1 row and will have only 1 row, so how would I do it so it automatically edits the only row in the table.
Do I use the * thing?
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
11-04-2009, 06:12 PM
|
Re: Change insert to update
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
If you know that at all times there will be only one row, you can just skip the WHERE clause, thereby applying the changes to all rows (in this case only one). So:
Code:
"UPDATE testing SET FirstName='$firstname', LastName='$lastname', MiddleName='$middlename'"
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
11-04-2009, 06:41 PM
|
Re: Change insert to update
|
Posts: 115
Name: Not Telling
|
Please do not spam my thread. 
__________________
█ MY MSN: Please login or register to view this content. Registration is FREE
█ PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
█ PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
|
|
|
|
|
« Reply to Change insert to update
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|