Jump to content


Photo

help with edit query


  • Please log in to reply
2 replies to this topic

#1 dhrups

dhrups
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 07 May 2006 - 03:28 PM

I am updating my table in the database, I have written the code, and keep getting the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Apache2\Apache2\htdocs\SCS\edit_customers.php on line 166

i have commented the line number 166 in bold.

can some one pls let me know if i am doing something wrong?

(the primary key is model_number and the type is varchar)
but i dont think this shud matter??

Script edit_model.php

<? php
require_once("config.php);
?>

<?

function escape_data($data){
if(ini_get('magic_quotes_gpc')){
$data = stripslashes($data);
}

if (function_exists('mysql_real_escape_string')){
global $connection;
$data = mysql_real_escape_string(trim($data), $connection);
} else {
$data = mysql_escape_string(trim($data));
}

return $data;

}

if ((isset($_GET['model_number']))){
($id = $_GET['model_number']);
}
elseif ((isset($_POST['model_number']))){
($id = $_POST['model_number']);
}
else {
echo '<h1 model_number="mainhead">PAGE ERROR</h1>
<p class="error">This page has been accessed in errorpuj.</p><p><br/><br/></p>';
exit();
}

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

$errors = array();


//check for first name
if(empty($_POST['category'])){
$error[] = 'You forgot to enter the category.';
} else {
$fn = escape_data($_POST['category']);
}


if(empty($_POST['quantity'])){
$error[] = 'You forgot to enter the quantity.';
} else {
$e = escape_data($_POST['quantity']);
}


if(empty($errors)) {


$query = "SELECT model_number FROM models WHERE quantity= '$e' AND model_number != $id";
$result = mysql_query($query);
if(mysql_num_rows($result)==0){

$query = "UPDATE models SET model_number='$id', category='$fn', quantity='$e' WHERE model_number=$id";

$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {

echo '<p> The machine detail has been edited.<p>';

} else {
echo '<h1 model_number="mainhead"> System Error</h1>
<p class = "error" > The machine detail could not be edited due to a system error.</p>';
echo '<p>' . mysql_error() . '<br/><br/> Query: ' . $query . '<p>';

exit();
}

}
else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The quantity had already been registered.</p>';
}

} else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The following error(s) occured:<br/>';
foreach ($errors as $msg){
echo " - $msg<br/>\n";
}

echo '</p><p>Please Try Again. </p><p><br/></p>';

}

}





$query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query);

if (mysql_num_rows($result) ==1){ [/u] //this is line no 166

$row = mysql_fetch_array ($result, MYSQL_NUM);


echo '<h2> Edit Customer </h2>
<form name="edit_cust_details.php" action = "edit_model.php" method = "post">
<p>Category: <input type="text" name ="category" size="15" maxlength="15" value="' .$row[0] . '"/></p>
<p>Model_number: <input type="text" name ="model_number" size="15" maxlength="15" value="' .$row[1] . '"/></p>
<p>Quantity: <input type="text" name ="quantity" size="15" maxlength="15" value="' .$row[2] . '"/></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="model_number" value="' . $id . '" />
</form>';


} else {
echo '<h1 model_number="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br/><br/></p>';
}

mysql_close();

?>





#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 May 2006 - 03:39 PM

You have an error in your sql query that is why your are retrieving that error. So change the following:
$query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query);

if (mysql_num_rows($result) ==1){
to
$query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query) or die("Query error: <i>" . $query . "</i><br><br>" . mysql_error());

if (mysql_num_rows($result) ==1){

When you run your edit script again PHP/MySQL will report a proper error message in the following format:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Query error: [query here]

[mysql error here][/quote]

#3 dhrups

dhrups
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 07 May 2006 - 04:14 PM

hi. thanks for ur help. i made the change to my sql statement, and now i am getting the following error:

Query error: SELECT category, model_number, quantity FROM models WHERE model_number=\'. $row[\'model_number\']. \'

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 '\'. $row[\'model_number\']. \'' at line 1

is there anything else wrong with my code?
like i sed i am using the primary key to be model_number which has a type varchar, as it contains both numbers and letters in it.
could that be the problem.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users