Page 1 of 1

need another set of eyes for this...

Posted: Tue Nov 29, 2005 12:35 pm
by joviyach
I am still quite a newbie here. I am working on a very simple PHP/MySQL app to catalog my CDs. This is for a school project. Anyway, I am having a couple of problems with this that I haven't been able to figure out. I will post the first one here...

I am getting a PARSE ERROR UNEXPECTED ')' in LINE 14 when I try to use this add info to the database. Is there a better way that I should have done this? I tried commenting out that block, but then I get other errors.

Code: Select all

<html>
<head>
  <title>CD Catalog</title>
</head>
<body>
<h1>CD Catalog Entry Results</h1>
<?php
  // create short variable names
  $title=$_POST['title'];
  $genre=$_POST['genre'];
  $band=$_POST['band'];


  if (!$title || !$genre || !$band ||)
  {
     echo 'You have not entered all the required details.<br />'
        .'Please go back and try again.';
     exit;
  }

  if (!get_magic_quotes_gpc())
  {
    $title = addslashes($title);
    $genre = addslashes($genre);
    $band = addslashes($band);

  }

  @ $db = new mysqli('localhost', '*****', '*****', 'cdcatalog');

  if (mysqli_connect_errno())
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  $query = "insert into cds values
            ('".$title."', '".$genre."', '".$band."')";
  $result = $db->query($query);
  if ($result)
      echo  $db->affected_rows.' data inserted into database.';

  $db->close();
?>
</body>
</html>

Posted: Tue Nov 29, 2005 12:43 pm
by jwalsh
you have a || (or) with nothing following it...

Code: Select all

if (!$title || !$genre || !$band ||)
change to

Code: Select all

if (!$title || !$genre || !$band )

Posted: Tue Nov 29, 2005 1:00 pm
by joviyach
That was simple. Thanks.

My other problem with this project, and this has been the big one is...

I can't get the data to post to the database, nor can I even get an error or reason why it's not. The script should produce either "data has been added" or "try again", but it does nothing but put the title on the screen. When I manually check the DB with PHPMyAdmin, no data has been added to the tables. If I manually add data to the table, my search script does the same thing that my add script does, which is nothing. Not even the error. Is there anyway to force this to give some kind of error at least so I can see why it's not posting to the DB?

thanks

Posted: Tue Nov 29, 2005 1:03 pm
by jwalsh

Code: Select all

$query = "insert into cds values
            ('".$title."', '".$genre."', '".$band."')";
You'll need a full insert syntax, what's your table structure?

Posted: Tue Nov 29, 2005 1:13 pm
by joviyach
db = "cdcatalog"

table = "cds"

fields

title char(25) unique
genre char(25)
band char(25)

Posted: Tue Nov 29, 2005 1:17 pm
by jwalsh
Hmm,

well, technically your insert should work, but you should create a primary key "ID" field. Also, you may want to Censor your real username and passwords when you post code :)

I'm not sure what the problem is. Try echoing $query after setting it.

Josh

Posted: Tue Nov 29, 2005 1:19 pm
by joviyach
D'oh! Forgot about the password, etc...

Thanks. I will give that a try and see what happens. It would be nice if it actually gave an error I could use.

thanks again!

Posted: Tue Nov 29, 2005 9:12 pm
by trukfixer
when debugging mysql queries, I have two pieces of advice:

First, assign all sql to a variable first , and second, following the query, echo mysql_error();

Example:

Code: Select all

//assume connection , etc....

$sql - "select * from this_table where my_value='$something'";
$Resource = mysql_query($sql);
echo mysql_error();
echo $sql;
It's always helpful to get mysql's error message- if there is none, then mysql didnt have an error, so then echoing $sql will show you what the actual query was, (maybe a variable or a typo causing teh value to be null?)

Posted: Tue Nov 29, 2005 9:22 pm
by neophyte
Adding on to trukfixer, if mysql_error() gets you no where try running the query in a db management interface like phpmyadmin until it works.

Posted: Tue Nov 29, 2005 10:03 pm
by josh
trukfixer wrote: First, assign all sql to a variable first , and second, following the query, echo mysql_error();
I got this from the zend study guide, I use it in all my code now

Code: Select all

$result = mysql_query(
      sprintf(
            "SELECT * FROM table WHERE field = '%s'",
            mysql_real_escape_string($value)
      )
) or die(mysql_error());
It really improves readability on huge SQL queries