ntroycondo Posted September 6, 2010 Share Posted September 6, 2010 Trying to get this to work but I know I'm missing something here. It's a form that is meant to update a record in DB. I am getting errore reference to first if (mysql_num_rows($result) == 1) in code and error message: Error has occurred. $connection = mysql_connect("xxxxxxxx","xxxxxx","xxxxxx"); if (!$connection) { die("Error connecting to database " . mysql_error()); } if (isset($_POST['submitted'])) $name = $_POST['name']; $email = $_POST['email']; $PHOTOGRAPHERID = $_POST['PHOTOGRAPHERID']; $query = "SELECT * FROM Photographers WHERE PHOTOGRAPHERID = $PHOTOGRAPHERID"; $result = mysql_query($query); if (mysql_num_rows($result) == 1){ // Make the query. $query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = @mysql_query ($query); // Run the query. if (mysql_affected_rows() == 1) { // If it ran OK. header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/"."search.php"); } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">The name could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. exit(); } } else { echo '<h1 id="mainhead">Error!</h1> <p class="error">Error has occurred.</p>'; } $query = "SELECT name, email FROM Photographer WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = @mysql_query ($query); // Run the query. // Create the form. echo '<h2>Edit a User</h2> <form action="update.php" method="post"> <p>Name: <input type="text" name="name" size="60" maxlength="60" value="' . $row[0] . '" /></p> <p>Email: <input type="text" name="email" size="60" maxlength="60" value="' . $row[1] . '" /></p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="PHOTOGRAPHERID" value="' . $id . '" /> </form>'; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 6, 2010 Share Posted September 6, 2010 If I'm not mistaken, you're missing a mysql_select_db() in that code. Quote Link to comment Share on other sites More sharing options...
ntroycondo Posted September 6, 2010 Author Share Posted September 6, 2010 Yeah, the mysql_select_db() was commented out in my notes. I've put it in there now but get same error. Quote Link to comment Share on other sites More sharing options...
Hypnos Posted September 6, 2010 Share Posted September 6, 2010 You don't have a bracket after "if (isset($_POST['submitted']))", so even when the form isn't submitted it's going to try to do the insert. Quote Link to comment Share on other sites More sharing options...
ntroycondo Posted September 7, 2010 Author Share Posted September 7, 2010 I've changed code around a little but still not working. But I've also been working at it way too long to. Now I get a error from the second ELSE statement: There has been an error in updating the database: 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 '' at line 1 <?php $connection = mysql_connect("xxxxxxxxxxxxx","xxxxxxxxxxxx", "xxxxxxxxxxxx"); if (!$connection) die("Error connecting to database " . mysql_error()); mysql_select_db('Photographers'); if (isset($_POST['submitted'])) { $name = $_POST['name']; $email = $_POST['email']; $PHOTOGRAPHERID = $_POST['PHOTOGRAPHERID']; } $query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = mysql_query($query); if($result){ echo "<p>The user has been updated.</p>"; }else{ echo "<p>There has been an error in updating the database: ".mysql_error()."</p>"; } $query = "SELECT name, email FROM Photographers WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = @mysql_query ($query); // Run the query. // Create the form. echo '<h2>Edit a User</h2> <form action="update.php" method="post"> <p>Name: <input type="text" name="name" size="60" maxlength="60" value="' . $row[0] . '" /></p> <p>Email: <input type="text" name="email" size="60" maxlength="60" value="' . $row[1] . '" /></p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="PHOTOGRAPHERID" value="' . $id . '" /> </form>'; ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 7, 2010 Share Posted September 7, 2010 If you change the the line with the query execution as below, you can see what the query string holds, and figure out which variable isn't getting set properly. $query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = mysql_query($query) or die( '<br />Query string: ' . $query . '<br />Produced error: ' . mysql_error() . '<br />'); 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.