TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-07-2009, 05:24 PM   #1 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default Updating data in mysql

Hello hope you can help me..

I have found this script on http://www.phpeasystep.com/mysql/9.html but it dosen't update the database. I don't get any errors so i can't find out what is wrong.

UPDATE.PHP:
<?php
$host="localhost"; // Host name
$username="xx"; // Mysql username
$password="xx"; // mysql password
$db_name="xx"; // Database name
$tbl_name="test_mysql"; // 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");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</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>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" 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>

<?

// close connection
mysql_close();

?>

UPDATE_AC.PHP:
<?php
$host="localhost"; // Host name
$username="xx"; // Mysql username
$password="xx"; // Mysql password
$db_name="xx"; // Database name
$tbl_name="test_mysql"; // 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");

// update data in mysql database
$sql = "UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

LIST_RECORDS.PHP:
<?php
$host="localhost"; // Host name
$username="xx"; // Mysql username
$password="xx"; // Mysql password
$db_name="xx"; // Database name
$tbl_name="test_mysql"; // 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);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['lastname']; ?></td>
<td><? echo $rows['email']; ?></td>

<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
cskott is offline  
Reply With Quote
Old 11-07-2009, 05:39 PM   #2 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

Put the line

PHP Code:
error_reporting(E_ALL); 
in as one of the first lines and see what error it gives.

Just a note, none of the data is being escaped; this means that I could submit a MySQL command like "DROP TABLE", and the entire database/table would be wiped clean. This code is very poorly written and I would advise you to steer clear of any code written by this author/site.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-07-2009, 06:25 PM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

I have put in error_reporting(E_ALL);
but it dosen't come with any errors?
cskott is offline  
Reply With Quote
Old 11-08-2009, 10:24 AM   #4 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

Is there not any who has an idea what the problem is?
cskott is offline  
Reply With Quote
Old 11-08-2009, 05:07 PM   #5 (permalink)
The Wanderer
 
Join Date: Nov 2009
Location: Denmark
Posts: 5
Thanks: 1
sLysdal is on a distinguished road
Default

try changing
PHP Code:
mysql_query($sql); 
to
PHP Code:
mysql_query($sql) or die(mysql_error()); 
what does that output?

and as adamdecaf said, the code can easily be injected
sLysdal is offline  
Reply With Quote
Old 11-08-2009, 08:00 PM   #6 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

I have put in mysql_query($sql) or die(mysql_error()); in UPDATE_AC.PHP but that didn't work..

What can i do so my code isn't so easy to enject?
I don't know so much about php so hope you can help me. Also to get the UPDATE working..
cskott is offline  
Reply With Quote
Old 11-08-2009, 08:19 PM   #7 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

Well, I would run mysql_real_escape_string() on every variable that is being placed into the mysql statement.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-08-2009, 08:23 PM   #8 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

and what would that mean?
Could you place an eksampel..
cskott is offline  
Reply With Quote
Old 11-09-2009, 12:56 AM   #9 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

An escape sample.

PHP Code:
$sql "UPDATE " mysql_real_escape_string($tbl_name) . 
" SET name=\'" mysql_real_escape_string($name) . 
"\', lastname=\'" mysql_real_escape_string($lastname) .
 
"\', email=\'" mysql_real_escape_string($email) . 
"\' WHERE id=\'" mysql_real_escape_string($id) . "\'"
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-09-2009, 10:15 AM   #10 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

I have put your eksampel in update_ac.php and then i get ERROR.


<?php
$host="localhost"; // Host name
$username="xx"; // Mysql username
$password="xx"; // Mysql password
$db_name="xx"; // Database name
$tbl_name="test_mysql"; // 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");

// update data in mysql database
$sql = "UPDATE " . mysql_real_escape_string($tbl_name) .
" SET name=\'" . mysql_real_escape_string($name) .
"\', lastname=\'" . mysql_real_escape_string($lastname) .
"\', email=\'" . mysql_real_escape_string($email) .
"\' WHERE id=\'" . mysql_real_escape_string($id) . "\'";

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>
cskott is offline  
Reply With Quote
Old 11-09-2009, 10:52 AM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

Right first off, there is no legitimate reason for wrapping your variables in double quotes when passing into a function (for example your mysql_connect etc), all it does is make php do extra work for no apparent reason.

PHP Code:
mysql_connect("$host" ... 
becomes:
PHP Code:
mysql_connect($host ... 
Secondly you don't call mysql_query at any point in your script, mysql can't read your mind.

Also in your sql, there is no need to escape the single quotes if the string is wrapped in doubles

heres a more or less cleaned version:

PHP Code:
<?php
$host     
'localhost';  // Host name
$username 'xx';         // Mysql username
$password 'xx';         // Mysql password
$db_name  'xx';         // Database name
$tbl_name 'test_mysql'// 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");

// update data in mysql database
$sql "UPDATE " mysql_real_escape_string($tbl_name) .
       
" SET name='" mysql_real_escape_string($name) .
       
"', lastname='" mysql_real_escape_string($lastname) .
       
"', email='" mysql_real_escape_string($email) .
       
"' WHERE id='" mysql_real_escape_string($id) . "'";

$result mysql_query($sql);
// if successfully updated.
if($result){
    echo 
"Successful";
    echo 
"<BR>";
    echo 
"<a href='list_records.php'>View result</a>";
}else {
    echo 
"ERROR";
}
also you might want to change
PHP Code:
echo "ERROR"
to somehting more useful like
PHP Code:
echo mysql_error(); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-09-2009, 11:08 AM   #12 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

?php
$host = 'localhost'; // Host name
$username = 'xx'; // Mysql username
$password = 'xx'; // Mysql password
$db_name = 'xx'; // Database name
$tbl_name = 'test_mysql'; // 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");

// update data in mysql database
$sql = "UPDATE " . mysql_real_escape_string($tbl_name) .
" SET name='" . mysql_real_escape_string($name) .
"', lastname='" . mysql_real_escape_string($lastname) .
"', email='" . mysql_real_escape_string($email) .
"' WHERE id='" . mysql_real_escape_string($id) . "'";

$result = mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}else {
echo mysql_error();
}


I have changed the file to this now.
It tell's me that the database is successfully updated, but it still doens't update the database?
cskott is offline  
Reply With Quote
Old 11-09-2009, 10:51 PM   #13 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

PHP Code:
// Take this out when you're ready to make the code live.
error_reporting(E_ALL);

// Connect to server and select database.
$hostname 'CHANGE ME';
$username 'CHANGE ME';
$password 'CHANGE ME';
$database 'CHANGE ME';

$link mysql_connect($host$username$password) || die(mysql_error());
// mysql_select_db($database, $link)or die(mysql_error());

// Set some default data.
$name 'John';
$lastname 'Doe';
$email 'user@example.com';
$id 123145;

// update data in mysql database
$sql "UPDATE " $database .
       
" SET name='" mysql_real_escape_string($name) .
       
"', lastname='" mysql_real_escape_string($lastname) .
       
"', email='" mysql_real_escape_string($email) .
       
"' WHERE id='" mysql_real_escape_string($id) . "'";

$result mysql_query($sql$link); 
Ok, wow, I was stupid and escaped $db_name. This will cause MySQL to try to update the data into a nonexistence/new database. Sorry!!

I would recommend the code in the post above me (sketchMedia's), or use this code, it's just easier for me to read. I was going through the code trying to fix any mundane bug and I found that I was escaping the table name.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-10-2009, 07:23 AM   #14 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

What about $tbl_name = 'test_mysql';
Shoulden't that be somewhere in the script?
cskott is offline  
Reply With Quote
Old 11-10-2009, 09:52 PM   #15 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 308
Thanks: 5
adamdecaf is on a distinguished road
Default

I changed it to $database, it really doesn't matter.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-11-2009, 06:35 AM   #16 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

I get this error now:

// Take this out when you're ready to make the code live. error_reporting(E_ALL); // Connect to server and select database. $hostname = 'localhost'; $username = 'xx'; $password = 'xx'; $database = 'xx'; $link = mysql_connect($host, $username, $password) || die(mysql_error()); // mysql_select_db($database, $link)or die(mysql_error()); // Set some default data. $name = 'John'; $lastname = 'Doe'; $email = 'user@example.com'; $id = 123145; // update data in mysql database $sql = "UPDATE " . $database . " SET name='" . mysql_real_escape_string($name) . "', lastname='" . mysql_real_escape_string($lastname) . "', email='" . mysql_real_escape_string($email) . "' WHERE id='" . mysql_real_escape_string($id) . "'"; $result = mysql_query($sql, $link);
cskott is offline  
Reply With Quote
Old 11-11-2009, 07:46 AM   #17 (permalink)
The Contributor
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 49
Thanks: 0
maeltar is on a distinguished road
Default

Looks like you are just copy/pasting what's been put on here....

Code:
$link = mysql_connect($host, $username, $password) || die(mysql_error());
// mysql_select_db($database, $link)or die(mysql_error());
change to

Code:
$link = mysql_connect($host, $username, $password) || die(mysql_error());
mysql_select_db($database, $link)or die(mysql_error());

did you put the code inside php tags ?

Code:
<?PHP
error_reporting(E_ALL);

// Connect to server and select database.
$hostname = 'CHANGE ME';
$username = 'CHANGE ME';
$password = 'CHANGE ME';
$database = 'CHANGE ME';

$link = mysql_connect($host, $username, $password) || die(mysql_error());
mysql_select_db($database, $link)or die(mysql_error());

// Set some default data.
$name = 'John';
$lastname = 'Doe';
$email = 'user@example.com';
$id = 123145;

// update data in mysql database
$sql = "UPDATE " . $database .
       " SET name='" . mysql_real_escape_string($name) .
       "', lastname='" . mysql_real_escape_string($lastname) .
       "', email='" . mysql_real_escape_string($email) .
       "' WHERE id='" . mysql_real_escape_string($id) . "'";

$result = mysql_query($sql, $link);

?>
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-11-2009, 03:49 PM   #18 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

No i forgot the php tags.. Sorry..
Now have put the tags in then it goes to a blank page when i press submit. No errors nothing.. The database is still not updated. Should i not tell the script what tabel in the database it should update.
cskott is offline  
Reply With Quote
Old 11-11-2009, 05:34 PM   #19 (permalink)
The Contributor
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 49
Thanks: 0
maeltar is on a distinguished road
Default

yep seen the mistake...

Code:
$sql = "UPDATE " . $database .
       " SET name='" . mysql_real_escape_string($name) .
       "', lastname='" . mysql_real_escape_string($lastname) .
       "', email='" . mysql_real_escape_string($email) .
       "' WHERE id='" . mysql_real_escape_string($id) . "'";
should read

Code:
$sql = "UPDATE " . $table_name .
       " SET name='" . mysql_real_escape_string($name) .
       "', lastname='" . mysql_real_escape_string($lastname) .
       "', email='" . mysql_real_escape_string($email) .
       "' WHERE id='" . mysql_real_escape_string($id) . "'";
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-12-2009, 06:27 AM   #20 (permalink)
The Wanderer
 
Join Date: Nov 2009
Posts: 13
Thanks: 0
cskott is on a distinguished road
Default

No it still goes to a blank page

<?PHP
// Take this out when you're ready to make the code live.
error_reporting(E_ALL);

// Connect to server and select database.
$hostname = 'localhost';
$username = 'xx';
$password = 'xx';
$database = 'xx';
$tbl_name = 'xx';

$link = mysql_connect($host, $username, $password) || die(mysql_error());
// mysql_select_db($database, $link)or die(mysql_error());



// update data in mysql database
$sql = "UPDATE " . $tbl_name .
" SET name='" . mysql_real_escape_string($name) .
"', lastname='" . mysql_real_escape_string($lastname) .
"', email='" . mysql_real_escape_string($email) .
"' WHERE id='" . mysql_real_escape_string($id) . "'";

$result = mysql_query($sql, $link);

?>
cskott is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
UPDATE MySQL data via web forms? Randy Absolute Beginners 26 05-07-2009 02:31 AM
having trouble updating with mysql sarmenhb Absolute Beginners 7 11-18-2008 06:49 AM
want to fetch data in excel sheet from my mysql database sharma.9.pooja Absolute Beginners 2 06-24-2008 05:04 AM
Log User Data with PHP & MySQL mortisimus Absolute Beginners 6 09-30-2007 05:52 PM


All times are GMT. The time now is 04:38 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design