Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Change insert to update
Old 11-04-2009, 02:25 PM Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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
PHP Code:
WHERE id='$id' 
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..
sith717 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-04-2009, 02:33 PM Re: Change insert to update
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-04-2009, 02:37 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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..
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 02:50 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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..
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:00 PM Re: Change insert to update
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
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'
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:04 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:12 PM Re: Change insert to update
Skilled Talker

Posts: 80
Name: John
Location: Sacramento
Trades: 0
Better: "...firstname = mysql_real_escape_string(trim(".$_POST['firstname']."))..."
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:16 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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..
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:26 PM Re: Change insert to update
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
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'
"
); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:33 PM Re: Change insert to update
Skilled Talker

Posts: 80
Name: John
Location: Sacramento
Trades: 0
If everything else is gravy it should. Note: You will need to have a viable connection open to the database of choice. For more:

Connect - http://www.php.net/manual/en/function.mysql-connect.php
Select - http://www.php.net/manual/en/functio...-select-db.php
Query - http://php.net/manual/en/function.mysql-query.php

Do Something (I suggest: mysql_fetch_row())

Close - http://www.php.net/manual/en/function.mysql-close.php
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:44 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:50 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 03:52 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 04:35 PM Re: Change insert to update
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-04-2009, 04:42 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
PHP Code:
<?php
include ('inc/config.php');
$firstname mysql_real_escape_string ($_POST['firstname']);
$lastname mysql_real_escape_string ($_POST['lastname']);
$middlenamemysql_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.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 06:12 PM Re: Change insert to update
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-04-2009, 06:41 PM Re: Change insert to update
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
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.
sith717 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Change insert to update
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.25778 seconds with 12 queries