Jump to content

[SOLVED] Unable to POST all the sudden...


jdorenbush

Recommended Posts

I was making a few changes to the admin side of a PHP based Bookstore I have. Somewhere along the lines I broke it and I am no PHP expert so I am having a heck of a time fixing it. I am unable to edit or add books to the database now. I keep getting the ERROR message. Here is the "Add Books" portion of the code. Does anyone see something that stands out as incorrect?

 

Code:

// Add Books
if ( $action == "AddBook" ) {
echo "
<form action=\"?action=doAddBook\" method=\"POST\">
<fieldset>
<legend>Add Book</legend>
<label for=\"book_cover\">Book Cover URL</label><input type=\"text\" name=\"book_cover\" class=\"txt\">
<label for=\"book_title\">Title</label><input type=\"text\" name=\"book_title\" class=\"txt\" >
<label for=\"book_author\">Author</label><input type=\"text\" name=\"book_author\" class=\"txt\" >
<label for=\"book_publisher\">Publisher</label><input type=\"text\" name=\"book_publisher\" class=\"txt\" >
<label for=\"book_publication\">Publication Year</label><input type=\"text\" name=\"book_publication\" class=\"txt\" >
<label for=\"book_isbn\">ISBN</label><input type=\"text\" name=\"book_isbn\" class=\"txt\" >
<label for=\"book_covertype\">Book Cover Type</label><select name=\"book_covertype\" class=\"txt\" >
<option selected> </option>
<option value=\"Hard\">Hard Cover</option>
<option value=\"Soft\">Soft Cover</option>
</select>
<label for=\"book_pages\">Pages</label><input type=\"text\" name=\"book_pages\" class=\"txt\" >
<label for=\"book_condition\">Condition</label><select name=\"book_condition\" class=\"txt\" ><option selected> </option>
<option value=\"New\">New</option>
<option value=\"Used/Like New\">Used/Like New</option>
<option value=\"Used/Slight Wear\">Used/Slight Wear</option>
<option value=\"Used/Worn\">Used/Worn</option>
</select>
<label for=\"book_category\">Category</label><select name=\"book_category\" class=\"txt\" >
<option selected> </option>";
$query = " SELECT * FROM categories ORDER BY cat_id ASC";
$result = mysql_query($query) or die('Error, query failed');
while ($getcat= mysql_fetch_array($result)) {
echo "<option value=\"$getcat[cat_name]\">$getcat[cat_name]</option>";}
echo "
</select><br /><br />
<label for=\"book_listprice\">List Price</label><input type=\"text\" name=\"book_listprice\" class=\"txt\" value=\"$\" >
<label for=\"book_amazonprice\">Amazon Price</label><input type=\"text\" name=\"book_amazonprice\" class=\"txt\" value=\"$\" >
<label for=\"book_price\">Mutineer Price</label><input type=\"text\" name=\"book_price\" class=\"txt\" value=\"$\" >
<label for=\"book_summary\">Book Summary</label><textarea name=\"book_summary\" cols=\"30\" rows=\"3\" class=\"txt\"></textarea>
<label for=\"book_whothis\">Who its For</label><textarea name=\"book_whothis\" cols=\"30\" rows=\"3\" class=\"txt\"></textarea>
<label for=\"book_opinion\">Mutineer's Opinion</label><textarea name=\"book_opinion\" cols=\"30\" rows=\"3\" class=\"txt\"></textarea>
<label for=\"book_approved\">Mutineer Approved?</label><br /><br />
Yes<input name=\"book_approved\" type=\"radio\" value=\"Yes\" />
No<input name=\"book_approved\" type=\"radio\" value=\"No\" /><br /><br />
<label for=\"book_essential\">Mutineer Essential?</label><br /><br />
Yes<input name=\"book_essential\" type=\"radio\" value=\"Yes\" />
No<input name=\"book_essential\" type=\"radio\" value=\"No\" /><br /><br />
<p><input type=\"Submit\" value=\"Submit\" class=\"txt2\"></p>
</fieldset>
</form> ";}

if ( $action == "doAddBook" ) {
$book_cover=mysql_real_escape_string($_POST['book_cover']);
$book_title=mysql_real_escape_string($_POST['book_title']);
$book_author=mysql_real_escape_string($_POST['book_author']);
$book_publisher=mysql_real_escape_string($_POST['book_publisher']);
$book_publication=mysql_real_escape_string($_POST['book_publication']);
$book_isbn=mysql_real_escape_string($_POST['book_isbn']);
$book_covertype=mysql_real_escape_string($_POST['book_covertype']);
$book_pages=mysql_real_escape_string($_POST['book_pages']);
$book_condition=mysql_real_escape_string($_POST['book_condition']);
$book_listprice=mysql_real_escape_string($_POST['book_listprice']);
$book_amazonprice=mysql_real_escape_string($_POST['book_amazonprice']);
$book_price=mysql_real_escape_string($_POST['book_price']);
$book_summary=mysql_real_escape_string($_POST['book_summary']);
$book_whothis=mysql_real_escape_string($_POST['book_whothis']);
$book_opinion=mysql_real_escape_string($_POST['book_opinion']);
$book_approved=mysql_real_escape_string($_POST['book_approved']);
$book_essential=mysql_real_escape_string($_POST['book_essential']);
$query = " INSERT INTO catalog VALUES('','$book_cover','$book_title','$book_author','$book_publisher','$book_publication','$book_isbn','$book_covertype','$book_pages','$book_condition','$book_listprice','$book_amazonprice','$book_price','$book_summary','$book_whothis','$book_opinion','$book_approved','$book_essential')";
$result = mysql_query($query) or die('ERROR: Unable to add book.');
echo "<h2>The book has been successfully added to the database.</h2>";  
}

 

 

MySQL: 4.1.20-3 / PHP: 4.3.9

Hosting: RackForce

 

Here is a screenshot from PHPMyAdmin:

phpmyadminqi1.png

 

Any help is much appreciated... Remember, take it easy on me - I am a PHP noob. :)

Link to comment
Share on other sites

You can leave out the columns if you supply all the values.

 

Change

<?php
$result = mysql_query($query) or die('ERROR: Unable to add book.');
?>

to

<?php
$result = mysql_query($query) or die("ERROR: Unable to add book. The query was: $query<br>" . mysql_error());
?>

 

This will show you the query and the mysql_error that caused the problem. Post the results.

 

Ken

Link to comment
Share on other sites

You can leave out the columns if you supply all the values.

 

Change

<?php
$result = mysql_query($query) or die('ERROR: Unable to add book.');
?>

to

<?php
$result = mysql_query($query) or die("ERROR: Unable to add book. The query was: $query<br>" . mysql_error());
?>

 

This will show you the query and the mysql_error that caused the problem. Post the results.

 

Ken

 

ERROR: Unable to update book. The query was: UPDATE catalog SET book_cover='bartendingfordummies.jpg',book_title='Bartending for Dummies', book_author='Ray Foley', book_publisher='IDG Books', book_publication='1997', book_isbn='0-7645-5051-9', book_covertype='', book_pages='', book_condition='', book_listprice='', book_amazonprice='', book_price='$9.99', book_summary='A basic guide to everything bartending and mixology.', book_whothis='Anyone interested in bartending or mixology; good starter book.', book_summary='A basic guide to everything bartending and mixology.', book_opinion='I’m typically not a fan of books in the “Dummies” series…not sure why, just not for some reason. This book, however, has been a useful tool for me over the years. If nothing else, it contains an accurate density chart for layering liquors and liqueurs. The book is laid out in an approachable and sensible way, and does a good job covering the basics of bartending, mixology, and spirits.', book_approved='', book_essential='', WHERE book_id=''
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 'WHERE book_id=''' at line 1

Link to comment
Share on other sites

All of a sudden you're showing an error with an Update, which is not in the code you showed.

 

Anyway, the problem here is that you have a comma before the "Where" clause which is incorrect. Also, the book_id is null, so if the update were to work, it wouldn't update the correct record.

 

Ken

Link to comment
Share on other sites

Whoops. That was the EDIT(UPDATE) portion. Here is what happens when I try and ADD.

 

ERROR: Unable to add book. The query was: INSERT INTO catalog VALUES('','test','test','test','test','test','test','Hard','test','New','$test','$test','$test','test','test','test','Yes','Yes')
Column count doesn't match value count at row 1

 

I fixed the comma in the EDIT portion, but how do I address the null ID?

Link to comment
Share on other sites

Try

 

<?php
mysql_query("INSERT INTO catalog (`column1``column2`,`column3`,`column4`,`column5`,`column6,`column7`,`column8,`column9`,`column10`,`column11`,`column12`,`column13`,`column14`,`column15`,`column16`,`column17`,`column17`)VALUES('','test','test','test','test','test','test','Hard','test','New','$test','$test','$test','test','test','test','Yes','Yes')")
?>

 

But replace column1 or whatever with the column name.

Link to comment
Share on other sites

As I said before, you do not have to include the column names [red]if[/red] you have values for all the columns listed. In this case, the error is telling you that you don't.

 

I fixed the comma in the EDIT portion, but how do I address the null ID?

For us to tell you that, we need to see the code for the update portion.

 

Ken

Link to comment
Share on other sites

 

I fixed the comma in the EDIT portion, but how do I address the null ID?

For us to tell you that, we need to see the code for the update portion.

 

Ken

 

if ( $action == "EditBook" ) {
  $id=$_GET['id'];
  $query = "SELECT * FROM catalog WHERE book_id = '$id'";
  $result = mysql_query($query) or die('Couldnt read book catalog database');
  $book_cover=mysql_result($result,$i,"book_cover");
  $book_title=mysql_result($result,$i,"book_title");
  $book_author=mysql_result($result,$i,"book_author");
  $book_publisher=mysql_result($result,$i,"book_publisher");
  $book_publication=mysql_result($result,$i,"book_publication");
  $book_isbn=mysql_result($result,$i,"book_isbn");
  $book_covertype=mysql_result($result,$i,"book_covertype");
  $book_pages=mysql_result($result,$i,"book_pages");
  $book_condition=mysql_result($result,$i,"book_condition");
  $book_listprice=mysql_result($result,$i,"book_listprice");
  $book_amazonprice=mysql_result($result,$i,"book_amazonprice");
  $book_price=mysql_result($result,$i,"book_price");
  $book_summary=mysql_result($result,$i,"book_summary");
  $book_whothis=mysql_result($result,$i,"book_whothis");
  $book_opinion=mysql_result($result,$i,"book_opinion");
  $book_approved=mysql_result($result,$i,"book_approved");
  $book_essential=mysql_result($result,$i,"book_essential");

  echo "
<form action=\"?action=doEditBook\" method=\"POST\">
<fieldset>
<legend>Edit Book with ID $id</legend>
<input type=\"hidden\" name=\"id\" value=\"$id\">				
<label for=\"book_cover\">Book Cover URL</label><input type=\"text\" name=\"book_cover\" class=\"txt\" value=\"$book_cover\" >
<label for=\"book_title\">Title</label><input type=\"text\" name=\"book_title\" class=\"txt\" value=\"$book_title\" >
<label for=\"book_author\">Author</label><input type=\"text\" name=\"book_author\" class=\"txt\" value=\"$book_author\" >
<label for=\"book_publisher\">Publisher</label><input type=\"text\" name=\"book_publisher\" class=\"txt\" value=\"$book_publisher\" >
<label for=\"book_publication\">Publication Year</label><input type=\"text\" name=\"book_publication\" class=\"txt\" value=\"$book_publication\" >
<label for=\"book_isbn\">ISBN</label><input type=\"text\" name=\"book_isbn\" class=\"txt\" value=\"$book_isbn\" >
<label for=\"book_covertype\">Book Cover Type</label><select name='book_covertype' class=\"txt\"value=\"$book_covertype\" ><option selected value=\"$book_covertype\"></option>
<option value=\"Hard\">Hard Cover</option>
<option value=\"Soft\">Soft Cover</option>
</select>
<label for=\"book_pages\">Pages</label><input type=\"text\" name=\"book_pages\" class=\"txt\" value=\"$book_pages\" >
<label for=\"book_condition\">Condition</label><select name='book_condition' class=\"txt\" ><option selected value=\"$book_condition\"></option>
<option value=\"New\">New</option>
<option value=\"Used/Like New\">Used/Like New</option>
<option value=\"Used/Slight Wear\">Used/Slight Wear</option>
<option value=\"Used/Worn\">Used/Worn</option>
</select>
<label for=\"book_category\">Category</label><select name='book_category' class=\"txt\"><option selected value=\"$book_category\"> </option>";
$query = " SELECT cat_name FROM categories";
$result = mysql_query($query) or die('Cant select categories table');
while ($getcat= mysql_fetch_array($result)) { ?>
<? echo "<option value=\"$getcat[cat_name]\""; ?>
<? if ($getcat[cat_name]=="$book_category"){ echo " selected>"; } else { echo ">"; } ?>
<? echo "$getcat[cat_name]</option>"; ?><? }
echo " 
</select><br /><br />
<label for=\"book_listprice\">List Price</label><input type=\"text\" name=\"book_listprice\" class=\"txt\" value=\"$book_listprice\" >
<label for=\"book_amazonprice\">Amazon Price</label><input type=\"text\" name=\"book_amazonprice\" class=\"txt\" value=\"$book_amazonprice\" >
<label for=\"book_price\">Mutineer Price</label><input type=\"text\" name=\"book_price\" class=\"txt\" value=\"$book_price\" >
<label for=\"book_summary\">Book Summary</label><textarea name='book_summary' cols='30' rows='3' class=\"txt\">$book_summary</textarea>
<label for=\"book_whothis\">Who its For</label><textarea name='book_whothis' cols='30' rows='3' class=\"txt\">$book_whothis</textarea>
<label for=\"book_opinion\">Mutineer's Opinion</label><textarea name='book_opinion' cols='30' rows='3' class=\"txt\">$book_opinion</textarea>
<label for=\"book_approved\"><font color=\"red\">Mutineer Approved?</font></label><br /><br />
Yes<input name=\"book_approved\" type=\"radio\" value=\"Yes\" />
No<input name=\"book_approved\" type=\"radio\" value=\"No\" /><br /><br />
<label for=\"book_essential\"><font color=\"red\">Mutineer Essential<?/font></label><br /><br />
Yes<input name=\"book_essential\" type=\"radio\" value=\"Yes\" />
No<input name=\"book_essential\" type=\"radio\" value=\"No\" /><br /><br />
<p><input type=\"Submit\" value=\"Submit\" class=\"txt2\"></p>
</fieldset>
</form>"; }

if ( $action == "doEditBook" ) {
$book_cover=mysql_real_escape_string($_POST['book_cover']);
$book_title=mysql_real_escape_string($_POST['book_title']);
$book_author=mysql_real_escape_string($_POST['book_author']);
$book_publisher=mysql_real_escape_string($_POST['book_publisher']);
$book_publication=mysql_real_escape_string($_POST['book_publication']);
$book_isbn=mysql_real_escape_string($_POST['book_isbn']);
$book_covertype=mysql_real_escape_string($_POST['book_covertype']);
$book_pages=mysql_real_escape_string($_POST['book_pages']);
$book_condition=mysql_real_escape_string($_POST['book_condition']);
$book_listprice=mysql_real_escape_string($_POST['book_listprice']);
$book_amazonprice=mysql_real_escape_string($_POST['book_amazonprice']);
$book_price=mysql_real_escape_string($_POST['book_price']);
$book_summary=mysql_real_escape_string($_POST['book_summary']);
$book_whothis=mysql_real_escape_string($_POST['book_whothis']);
$book_opinion=mysql_real_escape_string($_POST['book_opinion']);
$book_approved=mysql_real_escape_string($_POST['book_approved']);
$book_essential=mysql_real_escape_string($_POST['book_essential']);

$query = "UPDATE catalog SET book_cover='$book_cover',book_title='$book_title', book_author='$book_author', book_publisher='$book_publisher', book_publication='$book_publication', book_isbn='$book_isbn', book_covertype='$book_covertype', book_pages='$book_pages', book_condition='$book_condition', book_listprice='$book_listprice', book_amazonprice='$book_amazonprice', book_price='$book_price', book_summary='$book_summary', book_whothis='$book_whothis', book_summary='$book_summary', book_opinion='$book_opinion', book_approved='$book_approved', book_essential='$book_essential' WHERE book_id='$id'";
$result = mysql_query($query) or die("ERROR: Unable to update book. The query was: $query<br>" . mysql_error()); 
echo "<h2>The book has been successfully updated in the database.</h2>";  
}

Link to comment
Share on other sites

UPDATE: I've sort of fixed the EDIT portion of the admin. It was looking for ID when it should have been looking for BOOK_ID. So it UPDATES the record, but when I go back to view whats its supposedly updated, it shows me nothing that I edited.

 

Now I need to fix the ADD portion. It looks like the field I am missing is the BOOK_ID when I am trying to add a book. The ID is just supposed to auto-increment for each book I add. Where/what would I include so that it inserts an auto-incremented book ID # into the database for each book I add?

Link to comment
Share on other sites

Try:

 

<?php
if ( $action == "doEditBook" ) {
$book_cover=mysql_real_escape_string($_POST['book_cover']);
$book_title=mysql_real_escape_string($_POST['book_title']);
$book_author=mysql_real_escape_string($_POST['book_author']);
$book_publisher=mysql_real_escape_string($_POST['book_publisher']);
$book_publication=mysql_real_escape_string($_POST['book_publication']);
$book_isbn=mysql_real_escape_string($_POST['book_isbn']);
$book_covertype=mysql_real_escape_string($_POST['book_covertype']);
$book_pages=mysql_real_escape_string($_POST['book_pages']);
$book_condition=mysql_real_escape_string($_POST['book_condition']);
$book_listprice=mysql_real_escape_string($_POST['book_listprice']);
$book_amazonprice=mysql_real_escape_string($_POST['book_amazonprice']);
$book_price=mysql_real_escape_string($_POST['book_price']);
$book_summary=mysql_real_escape_string($_POST['book_summary']);
$book_whothis=mysql_real_escape_string($_POST['book_whothis']);
$book_opinion=mysql_real_escape_string($_POST['book_opinion']);
$book_approved=mysql_real_escape_string($_POST['book_approved']);
$book_essential=mysql_real_escape_string($_POST['book_essential']);
$book_id=mysql_real_escape_string($_POST['id']); // Added this, since you've put your id in a hidden field, it needs a variable assigned to it (since using $_POST['id'] can get complicated)

$query = "UPDATE catalog SET book_cover='$book_cover',book_title='$book_title', book_author='$book_author', book_publisher='$book_publisher', book_publication='$book_publication', book_isbn='$book_isbn', book_covertype='$book_covertype', book_pages='$book_pages', book_condition='$book_condition', book_listprice='$book_listprice', book_amazonprice='$book_amazonprice', book_price='$book_price', book_summary='$book_summary', book_whothis='$book_whothis', book_summary='$book_summary', book_opinion='$book_opinion', book_approved='$book_approved', book_essential='$book_essential' WHERE book_id='$book_id'";
$result = mysql_query($query) or die("ERROR: Unable to update book. The query was: $query<br>" . mysql_error()); 
echo "<h2>The book has been successfully updated in the database.</h2>";  
} ?>

Link to comment
Share on other sites

 

Now I need to fix the ADD portion. It looks like the field I am missing is the BOOK_ID when I am trying to add a book. The ID is just supposed to auto-increment for each book I add. Where/what would I include so that it inserts an auto-incremented book ID # into the database for each book I add?

 

Suppose your book records are

 

book_id | book_cover | book_title | book_author

 

you can either supply all column names except "book_id"

INSERT INTO book (book_cover, book_title, book_author) VALUES  ('x', 'y', 'z')

 

or use a null value for the auto_inc id so all values are still provided in the correct order

INSERT INTO book VALUES  (null, 'x', 'y', 'z')

 

edit: P.S. or you can do both, of course

INSERT INTO book (book_id, book_cover, book_title, book_author) VALUES  (null, 'x', 'y', 'z')

Link to comment
Share on other sites

Since you've named your form fields the same as the database fields you can let PHP do the work of creating the correct query:

<?php
if ($action == 'doEditBook') {
    $qtmp = array();
    foreach ($_POST as $k => $v)
        if ($k != 'submit' && $k != 'id')
            $qtmp[] = $k . " = '" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
    $query = "UPDATE catalog SET " . implode(', ',$qtmp) . " WHERE book_id='" . mysql_real_escape_string($_POST['id']) . "'";
    $result = mysql_query($query) or die("ERROR: Unable to update book. The query was: $query<br>" . mysql_error()); 
    echo "<h2>The book has been successfully updated in the database.</h2>";  
} ?>

 

Ken

 

Link to comment
Share on other sites

Since you've named your form fields the same as the database fields you can let PHP do the work of creating the correct query:

<?php
if ($action == 'doEditBook') {
    $qtmp = array();
    foreach ($_POST as $k => $v)
        if ($k != 'submit' && $k != 'id')
            $qtmp[] = $k . " = '" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
    $query = "UPDATE catalog SET " . implode(', ',$qtmp) . " WHERE book_id='" . mysql_real_escape_string($_POST['id']) . "'";
    $result = mysql_query($query) or die("ERROR: Unable to update book. The query was: $query<br>" . mysql_error()); 
    echo "<h2>The book has been successfully updated in the database.</h2>";  
} ?>

 

Ken

 

 

Hey it worked! Very compact code to. It looks great, I don't fully understand it, but in time I will make sense of it. Is it possible to use something along those lines for doAddBook?

Link to comment
Share on other sites

This is what I came up with...

if ($action == 'doAddBook') {
  $qtmp = array();
    foreach ($_POST as $k => $v)
        if ($k != 'submit' && $k != 'id')
        $qtmp[] = $k . " = '" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
        $query = "INSERT catalog SET " . implode(', ',$qtmp) . mysql_real_escape_string($_POST['id']) . "'";
        $result = mysql_query($query) or die("ERROR: Unable to add book. The query was: $query<br />" . mysql_error()); 
        echo "<h2>The book has been successfully added to the database.</h2>";  
}

 

I received an error though...

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

 

I don't really understand this script so its really difficult for me to try and solve the problem.

Link to comment
Share on other sites

You're not creating the correct query. Try:

<?php
if ($action == 'doAddBook') {
  $qtmp = array();
  foreach ($_POST as $k => $v)
      if ($k != 'submit')
        $qtmp[] = $k . " = '" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
      $query = "INSERT catalog SET " . implode(', ',$qtmp);
      $result = mysql_query($query) or die("ERROR: Unable to add book. The query was: $query<br />" . mysql_error()); 
      echo "<h2>The book has been successfully added to the database.</h2>";  
}?>

 

Ken

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.