Jump to content

[SOLVED] mysql edit update


justAnoob

Recommended Posts

little script here to edit mysql row data,,It will echo "Thank you! Information updated."  But when I check my database,, the info never changed...  Also, check the commented line below.

 

<?php
include "connection.php";
$id=$r["id"];

if ($_POST["submit"])
{
$name = $_POST["item_name"];
$description = $_POST["description"];
$seeking = $_POST["in_return"];

$sql = "UPDATE $table1 SET item_name = '$name' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.";
echo $id;  // tried to echo the row id,, but nothing comes up
}
?>

 

Here is one of the input boxes that I have on the form.

<?php
<input type="name" value="' . $row['item_name'] . '" size="50" />
?>

 

Link to comment
Share on other sites

  • Replies 55
  • Created
  • Last Reply

Top Posters In This Topic

Your HTML is broken.

Your <input> example has an incorrect type value.

http://www.w3schools.com/TAGS/tag_input.asp

 

You probably wanted something like this:

<input type="type" name="item_name" value="<?php echo $row['item_name']; ?>" size="50" />

 

Also, I don't know where $r['id'] comes from, so I can't really help you there..

As for the "thank you" message being display, you could add a check to see how many rows where updated and display it only if a row was successfully changed, rather than after it queries (regardless of success).

Link to comment
Share on other sites

Still having problems,,, can anyone help,, this is driving me crazy... I search everywhere for an answer and nothing.

<?php
session_start();
include "connection.php";
$getthatname = $_REQUEST['item_name'];
$gotit = $getthatname;
if ($_POST["submit"])
{
   $item_name = mysql_real_escape_string($_POST["item_name"]);
   $description = mysql_real_escape_string($_POST["description"]);
   $in_return = mysql_real_escape_string($_POST["in_return"]);
  
   mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'");
   echo "$gotit has been updated.";
   mysql_close();
}
?>

 

Here is the table that I'm working from......And yes my table is inside a form(form2) and my action is set....

The script echos that it was updated. But when I return to my table on my site,,, the values of the database have not been changed...

Link to comment
Share on other sites

1. Possible SQL injection on $gotit.

2. Change this -

mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'");

 

to

 

mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'") or die(mysql_error());

 

Any errors from that?

Link to comment
Share on other sites

<form id="form2" name="form2" method="post" action="updatetrade.php">
<?php
session_start();
include "connection.php";
$findit = $_SESSION['id'];
$result = mysql_query("SELECT id FROM members WHERE username = '$findit'");
$result = mysql_result($result, 0, 0);
$sql = mysql_query("SELECT imgpath, imgpath2, imgpath3, imgpath4, imgpath5, item_name, description, in_return FROM abcxyz WHERE user_id = '$result'");

$img2query = mysql_query("SELECT imgpath2 FROM abcxyz WHERE user_id = '$result'");
$num_rows = mysql_num_rows($img2query); 

echo "<table width='954' height='523' border='0' align='center' cellpadding='0' cellspacing='0' bordercolor='#000000' bgcolor='#BBD4E1'>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr><td width='188' height='180'><div align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath2'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath3'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath4'] . '" width="125" alt="" />';
echo "</div></td><td width='190'><div align='center'>";
echo '<img src="' . $row['imgpath5'] . '" width="125" alt="" />';
echo "</div></td></tr><tr><td height='43' colspan='4'>";
echo '<strong>Item Name:</strong> <input type="text" name="item_name" value="' . $row['item_name'] . '" size="50" />';
echo "</td><td><div align='center'>";
echo '<input type="submit" name="submit" id="submit" value="Update Trade">';
echo "</div></td></tr><tr><td height='116' colspan='4'>";
echo '<strong>Description:</strong> <textarea name="description" cols="75" rows="5">' . $row['description'] . '</textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td height='124' colspan='4'>";
echo '<strong>Seeking:</strong>       <textarea name="in_return" cols="75" rows="5">' . $row['in_return'] . '   	      </textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td colspan='4'>";
//echo "save delete buttons";
echo "</td><td><div align='center'>";
echo "</td></tr>";
}
echo "</table>";
?>
</form>

Link to comment
Share on other sites

1. Is there a reason you're alternating single quotes and double quotes for the HTML echo? For xHTML-compliancy, you should start echo with a single quote and double quote the attribute of the HTML tag.

 

2. What's the purpose of this:

$img2query = mysql_query("SELECT imgpath2 FROM abcxyz WHERE user_id = '$result'");
$num_rows = mysql_num_rows($img2query); 

 

3. What's the problem you're having? I don't see an UPDATE query.

Link to comment
Share on other sites

those 2 lines that you mentioned shouldn't have been there.. that was from something that I was working on earlier... there gone now.... here you go...

<form id="form2" name="form2" method="post" action="updatetrade.php">
<?php
session_start();
include "connection.php";
$findit = $_SESSION['id'];
$result = mysql_query("SELECT id FROM members WHERE username = '$findit'");
$result = mysql_result($result, 0, 0);
$sql = mysql_query("SELECT imgpath, imgpath2, imgpath3, imgpath4, imgpath5, item_name, description, in_return FROM abcxyz WHERE user_id = '$result'");

echo "<table width='954' height='523' border='0' align='center' cellpadding='0' cellspacing='0' bordercolor='#000000' bgcolor='#BBD4E1'>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr><td width='188' height='180'><div align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath2'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath3'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath4'] . '" width="125" alt="" />';
echo "</div></td><td width='190'><div align='center'>";
echo '<img src="' . $row['imgpath5'] . '" width="125" alt="" />';
echo "</div></td></tr><tr><td height='43' colspan='4'>";
echo '<strong>Item Name:</strong> <input type="text" name="item_name" value="' . $row['item_name'] . '" size="50" />';
echo "</td><td><div align='center'>";
echo '<input type="submit" name="submit" id="submit" value="Update Trade">';
echo "</div></td></tr><tr><td height='116' colspan='4'>";
echo '<strong>Description:</strong> <textarea name="description" cols="75" rows="5">' . $row['description'] . '</textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td height='124' colspan='4'>";
echo '<strong>Seeking:</strong>       <textarea name="in_return" cols="75" rows="5">' . $row['in_return'] . '   	      </textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td colspan='4'>";
//echo "save delete buttons";
echo "</td><td><div align='center'>";
echo "</td></tr>";
}
echo "</table>";
?>
</form>

Link to comment
Share on other sites

that is old,,, got in my post somehow by accident... this is the new script.....

<?php
session_start();
include "connection.php";
$getthatname = $_REQUEST['item_name'];
$gotit = $getthatname;
if ($_POST["submit"])
{
   $item_name = mysql_real_escape_string($_POST["item_name"]);
   $description = mysql_real_escape_string($_POST["description"]);
   $in_return = mysql_real_escape_string($_POST["in_return"]);
  
   mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'") or die(		  		       mysql_error());
   echo "$gotit has been updated.";
   mysql_close();
}
?>

I get no errors,, and it echos that it is updated.. But when I go back to my page that has my table with the mysql info.. everything is the same.. So somewher in this script it is not posting the information that I changed on my form.

Link to comment
Share on other sites

Try this:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
session_start();
require 'connection.php';

if(isset($_POST['submit'])) {

  $item_name = mysql_real_escape_string($_POST['item_name']);
  $description = mysql_real_escape_string($_POST['description']);
  $in_return = mysql_real_escape_string($_POST['in_return']);
  
  $sql = "UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$item_name'";
  
  if(mysql_query($sql)) {
    echo $item_name . ' has been updated.';
  }else{
    echo 'There was an error processing the query: ' . mysql_error();
  }


}else{//if form has been submitted to this page

echo '//maybe you could put form code here';

}

?>

 

Don't use $_REQUEST as it's depreciated.  Technically if there was an error, ken's previous mysql modification would have caught it, but perhaps you don't have error reporting on.  If you still don't see error after this, try pasting that mysql query directly into your mysql console / phpmyadmin and see what it say (with values filled in of course).

Link to comment
Share on other sites

If you mean to execute this line inside of mysql

 

$sql = "UPDATE member_trades SET item_name = 'blah', description = 'blah blah', in_return = '$blah blah blah' WHERE item_name = '$item_name'";

 

I got an error saying to check your sql syntax that corresponds with mysql version

Link to comment
Share on other sites

So you have this query:

mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'")

But yet the database does not update, even when it says the query ran, according to PHP.

 

So take this query line:

UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'

CHANGE ALL $values to be valid data, DO NOT LEAVE THIS ABOVE LINE AS IS, FIX IT TO CONTAIN REAL VALUES

 

And paste it into your phpMyAdmin sql interface, or a mysql console screen if it's hosted by you...  What I'm saying is, don't use PHP, go directly to MYSQL and test that query to make sure it's doing what you expect so that we can rule out mysql as the problem.

Link to comment
Share on other sites

alright,, I posted that back into php and put some new values and that worked out great...So I guess the problem is somewhere in the variables from my text field or text boxes........or the problem is where I'm selecting the item_name.

Link to comment
Share on other sites

Well,,, it works kinda... I can update and change the description and the in_return fields as long as I don't change the item_name... Why,, well when I change the item_name the script is looking for the changed item_name in my database which it won't find...So I'll try and work it out now.. If anyone has any ideas,,, please feel free... And a big thanks to everyone that helped.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.