digitalmartyr Posted February 19, 2008 Share Posted February 19, 2008 I have an edit page for products on a website. when you click on edit to edit an item, it brings you to the edit page, and it echos out all the information on the db about the item. that part of the querys work. i have it that on submit, to process the form for the updates and the echo out the result, when it processes the updates it errors with this message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/rock925/admin/editproduct.php on line 136 here is my code <?php session_start(); require('dbconnect.php'); require('functions.php'); if(!$_POST['submitUpdate']){ //if it hasnt been posted, show selected entry. $productId = (isset($_REQUEST['productid']))?$_REQUEST['productid']:""; //if productid is a number, its legit, so get it from the database if(preg_match("/^[0-9]+$/", $productId)) { $viewSql = "SELECT productname, productprice, productdscrb, productimg, available, category FROM products WHERE productid='$productId'"; $viewResult = mysql_query($viewSql)or die ("Error in query: $viewSql. " . mysql_error()); if(mysql_num_rows($viewResult) > 0){ $myproduct = mysql_fetch_array($viewResult); $productTitle = $myproduct['productname']; $productPrice = $myproduct['productprice']; $productCategory = $myproduct['category']; $productAvilb = $myproduct['available']; $productImg = $myproduct['productimg']; $productDscrb = stripslashes($myproduct['productdscrb']); $productDscrb = trim($productDscrb); $updateDscrb = str_replace("'","",$updateDscrb); $html = '<div id="productThumb">'; $html .= '<h3>product id: '.$productId.'</h3>'; $html .= '<h2>product name: '.$productTitle.'</h2>'; $html .= '<img height="140" width="100" src="'.$productImg.'"</img>'; $html .= '<br />'; $html .= '<span>Product Describtion '.$productDscrb.'</span>'; $html .= '</div>'; echo $html; } } else { $message = "no products match that id"; } ?> <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'].'?page=editprod&productid='.$productId.';' ?>"> <span> <p class="form1"> <input type="hidden" name="productid" value="<?php echo $productId; ?>"> <label for="productname">Product Name:</label> <input type="text" name="productname" value="<?php if(isset($productTitle)){ echo $productTitle;} ?>"/> </p> </p> <p class="form1"> <label for="productname">Product Price:</label> <input type="text" name="productprice" value='<?php if(isset($productPrice)){ echo $productPrice;} ?>'/> </p> <h3>Product Available</h3> <p class="form1"> <input id="avil_yes" checked="checked" class="radio" name="avil" type="radio" value="yes" /> <lable for="avil_yes">Yes</lable> </p> <p class="form1"> <input id="avil_no" class="radio" name="avil" type="radio" value="no" /> <lable for="avil_no">No</lable> </p> <p> <h3>Product Category</h3> <select name="productCat" id="productCat"> <?php $getCats = "SELECT * FROM categories"; $catResult = mysql_query($getCats)or die(mysql_error()); while($cats = mysql_fetch_array($catResult)) { //$selected = ($cats['catid'] == $catid)? 'selected':''; echo "<option value=".$cats['catname'].">".$cats['catname']."</option>"; } ?> </select> </p> <p class="form1"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <lable id="productimage">Product image:</label> <input type="file" name="productimage" value='<?php if(isset($productImg)){ echo $productImg;} ?>' > <p class="form1"> <label for="productname">Product Description:</label> <textarea type="textarea" name="productdesc" rows='10' cols='50'/> '<?php if(isset($productDscrb)){ echo $productDscrb;} ?>' </textarea> </p> <br /> <div id="subdiv"> <p class="submitarea"> <input type="submit" name="submitUpdate"/> </p> </div> </form> </fieldset> <?php }else{ //if pressed submit. require('dbconnect.php'); $productId = $_POST['productid']; $updateTitle = cleanentry((isset($_POST['productname'])))?$_POST['productname']:""; $updatePrice = cleanentry((isset($_POST['productprice'])))?$_POST['productprice']:""; $updateDscrb = cleanentry((isset($_POST['productdesc'])))?$_POST['productdesc']:""; $updateDscrb = stripslashes($updateDscrb); $updateDscrb = trim($updateDscrb); $updateDscrb = str_replace("'","",$updateDscrb); $updateImg = cleanentry((isset($_POST['productimage'])))?$_POST['productimage']:""; $updateAvilb = cleanentry((isset($_POST['avil'])))?$_POST['avil']:""; $updateCategory = cleanentry((isset($_POST['productCat'])))?$_POST['productCat']:""; $updateCategory = str_replace("'"," ", $updateCategory); $updateSql = "UPDATE products SET productname = '$updateTitle', productprice = '$updatePrice', productdscrb = '$updateDscrb', available = '$updateAvilb', category = '$updateCategory' WHERE productid = '$productId'"; echo $updateSql; $updateQuery = mysql_query($updateSql); $info = mysql_fetch_array($updateQuery); { $html = '<div id="productThumb">'; $html .= '<h3>product id: '.$info['productid'].'</h3>'; $html .= '<h2>product name: '.$info['productname'].'</h2>'; //$html .= '<img height="140" width="100" src="'.$info['productimg'].'"</img>'; $html .= '<br />'; $html .= '<span>Product Describtion '.$info['productdscrb'].'</span>'; $html .= "<a href='{$_SERVER['PHP_SELF']}?page=editprod&productid=".$info['productid'].">edit</a>"; $html .= '</div>'; echo $html; } } ?> i hope someone can help me with this, because i dont see where im going wrong thankyou very much Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 could you try this: if(mysql_num_rows($viewResult) > 0){ mysql_data_seek($viewResult); $myproduct = mysql_fetch_array($viewResult); see if it makes a difference Quote Link to comment Share on other sites More sharing options...
digitalmartyr Posted February 19, 2008 Author Share Posted February 19, 2008 ok, i tried the mysql_data_seek() and i still got the same error. when i update the entry thats displayed i get this warning: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource so here is my code, im having issues with the second sql query which is to update the current viewed product. <?php session_start(); require('dbconnect.php'); require('functions.php'); if(!$_POST['submitUpdate']){ //if it hasnt been posted, show selected entry. $productId = (isset($_REQUEST['productid']))?$_REQUEST['productid']:""; //if productid is a number, its legit, so get it from the database if(preg_match("/^[0-9]+$/", $productId)) { $viewSql = "SELECT productname, productprice, productdscrb, productimg, available, category FROM products WHERE productid='$productId'"; $viewResult = mysql_query($viewSql)or die ("Error in query: $viewSql. " . mysql_error()); if(mysql_num_rows($viewResult) > 0){ $myproduct = mysql_fetch_array($viewResult); $productTitle = $myproduct['productname']; $productPrice = $myproduct['productprice']; $productCategory = $myproduct['category']; $productAvilb = $myproduct['available']; $productImg = $myproduct['productimg']; $productDscrb = stripslashes($myproduct['productdscrb']); $productDscrb = trim($productDscrb); $updateDscrb = str_replace("'","",$updateDscrb); $html = '<div id="productThumb">'; $html .= '<h3>product id: '.$productId.'</h3>'; $html .= '<h2>product name: '.$productTitle.'</h2>'; $html .= '<img height="140" width="100" src="'.$productImg.'"</img>'; $html .= '<br />'; $html .= '<span>Product Describtion '.$productDscrb.'</span>'; $html .= '</div>'; echo $html; } } else { $message = "no products match that id"; } ?> <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'].'?page=editprod&productid='.$productId; ?>"> <span> <p class="form1"> <input type="hidden" name="productid" value="<?php echo $productId; ?>"> <label for="productname">Product Name:</label> <input type="text" name="productname" value="<?php if(isset($productTitle)){ echo $productTitle;} ?>"/> </p> </p> <p class="form1"> <label for="productname">Product Price:</label> <input type="text" name="productprice" value='<?php if(isset($productPrice)){ echo $productPrice;} ?>'/> </p> <h3>Product Available</h3> <p class="form1"> <input id="avil_yes" checked="checked" class="radio" name="avil" type="radio" value="yes" /> <lable for="avil_yes">Yes</lable> </p> <p class="form1"> <input id="avil_no" class="radio" name="avil" type="radio" value="no" /> <lable for="avil_no">No</lable> </p> <p> <h3>Product Category</h3> <select name="productCat" id="productCat"> <?php $getCats = "SELECT * FROM categories"; $catResult = mysql_query($getCats)or die(mysql_error()); while($cats = mysql_fetch_array($catResult)) { //$selected = ($cats['catid'] == $catid)? 'selected':''; echo "<option value=".$cats['catname'].">".$cats['catname']."</option>"; } ?> </select> </p> <p class="form1"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <lable id="productimage">Product image:</label> <input type="file" name="productimage" value='<?php if(isset($productImg)){ echo $productImg;} ?>' > <p class="form1"> <label for="productname">Product Description:</label> <textarea type="textarea" name="productdesc" rows='10' cols='50'/> '<?php if(isset($productDscrb)){ echo $productDscrb;} ?>' </textarea> </p> <br /> <div id="subdiv"> <p class="submitarea"> <input type="submit" name="submitUpdate"/> </p> </div> </form> </fieldset> <?php }else{ //if pressed submit. $productId = $_POST['productid']; $updateTitle = cleanentry((isset($_POST['productname'])))?$_POST['productname']:""; $updatePrice = cleanentry((isset($_POST['productprice'])))?$_POST['productprice']:""; $updateDscrb = cleanentry((isset($_POST['productdesc'])))?$_POST['productdesc']:""; $updateDscrb = stripslashes($updateDscrb); $updateDscrb = trim($updateDscrb); $updateDscrb = str_replace("'","",$updateDscrb); $updateImg = cleanentry((isset($_POST['productimage'])))?$_POST['productimage']:""; $updateAvilb = cleanentry((isset($_POST['avil'])))?$_POST['avil']:""; $updateCategory = cleanentry((isset($_POST['productCat'])))?$_POST['productCat']:""; $updateCategory = str_replace("'"," ", $updateCategory); $file_dir = "../assets/productimages"; //designate file upload folder foreach($_FILES as $file_name => $file_array) { echo "name: ".$file_array['name']."<br>\n"; //file location $file_loc = $file_dir . "/" . $file_array['name']; if(is_uploaded_file($file_array['tmp_name'])) { move_uploaded_file($file_array['tmp_name'], $file_loc) or die ("couldnt copy file"); echo "file was moved!<br>"; } } // create query $updateSql = "UPDATE products SET productname = '$updateTitle', productprice = '$updatePrice', productdscrb = '$updateDscrb', productimg = '$file_loc', available = '$updateAvilb', category = '$updateCategory' WHERE productid = '$productId';"; echo $updateSql; $updateQuery = mysql_query($updateSql); if(mysql_num_rows($updateQuery) > 0){ mysql_data_seek($updateQuery); $info = mysql_fetch_array($updateQuery); { $html = '<div id="productThumb">'; $html .= '<input type="hidden" name="productid" value="<?php echo $productId; ?>">'; $html .= '<h3>product id: '.$info['productid'].'</h3>'; $html .= '<h2>product name: '.$info['productname'].'</h2>'; $html .= '<img height="140" width="100" src="'.$info['productimg'].'"</img>'; $html .= '<br />'; $html .= '<span>Product Describtion '.$info['productdscrb'].'</span>'; $html .= "<a href='{$_SERVER['PHP_SELF']}?page=editprod&productid=".$info['productid'].">edit</a>"; $html .= '</div>'; echo $html; } } } ?> thank you. chad Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 whenever you use mysql_query its a good idea to add an "or die()" function like you did with your select statement earlier in the code, use this: $updateQuery = mysql_query($updateSql) or die("MySQL Query: $updateSql <br>".mysql_error()); tell us if it errors out. ------- to fix your num_rows error you need to use mysql_affected_rows($updateQuery); as num rows deals with result sets from SELECT and SHOW statements. Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 for more information please visit http://uk2.php.net/manual/en/function.mysql-affected-rows.php Quote Link to comment Share on other sites More sharing options...
digitalmartyr Posted February 19, 2008 Author Share Posted February 19, 2008 ok so i tried the affected rows function and now, the information updates, but i still get an error message.... Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/rock925/admin/editproduct.php on line 156 here is my code, where i kick the update query to mysql, and process the results. anything else i can try? <?php }else{ //if pressed submit. $productId = $_POST['productid']; $updateTitle = cleanentry((isset($_POST['productname'])))?$_POST['productname']:""; $updatePrice = cleanentry((isset($_POST['productprice'])))?$_POST['productprice']:""; $updateDscrb = cleanentry((isset($_POST['productdesc'])))?$_POST['productdesc']:""; $updateDscrb = stripslashes($updateDscrb); $updateDscrb = trim($updateDscrb); $updateDscrb = str_replace("'","",$updateDscrb); $updateImg = cleanentry((isset($_POST['productimage'])))?$_POST['productimage']:""; $updateAvilb = cleanentry((isset($_POST['avil'])))?$_POST['avil']:""; $updateCategory = cleanentry((isset($_POST['productCat'])))?$_POST['productCat']:""; $updateCategory = str_replace("'"," ", $updateCategory); $file_dir = "../assets/productimages"; //designate file upload folder foreach($_FILES as $file_name => $file_array) { echo "name: ".$file_array['name']."<br>\n"; //file location $file_loc = $file_dir . "/" . $file_array['name']; if(is_uploaded_file($file_array['tmp_name'])) { move_uploaded_file($file_array['tmp_name'], $file_loc) or die ("couldnt copy file"); echo "file was moved!<br>"; } } // create query $updateSql = "UPDATE products SET productname = '$updateTitle', productprice = '$updatePrice', productdscrb = '$updateDscrb', productimg = '$file_loc', available = '$updateAvilb', category = '$updateCategory' WHERE productid = '$productId';"; //echo $updateSql; $updateResult = mysql_query($updateSql)or die ("Error in query: $updateSql. " . mysql_error()); if(mysql_affected_rows($updateResult) > 0){ $updatedInfo = mysql_fetch_array($updateResult); { $html = '<div id="productThumb">'; $html .= '<input type="hidden" name="productid" value="<?php echo $productId; ?>">'; $html .= '<h3>product id: '.$updatedInfo['productid'].'</h3>'; $html .= '<h2>product name: '.$updatedInfo['productname'].'</h2>'; $html .= '<img height="140" width="100" src="'.$updatedInfo['productimg'].'"</img>'; $html .= '<br />'; $html .= '<span>Product Describtion '.$info['productdscrb'].'</span>'; $html .= "<a href='{$_SERVER['PHP_SELF']}?page=editprod&productid=".$updatedInfo['productid'].">edit</a>"; $html .= '</div>'; echo $html; } } } ?> Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 Sorry my bad try mysql_affected_rows(); if not what is the variable you used when initiating the connection to mysql? -- Also i do not think mysql_fetch_array($updateResult) will work as an UPDATE query does not return any rows (it literally just updates data, doesn't find anything), you may need to create another query with a SELECT statement. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.