need another set of eyes for this...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

need another set of eyes for this...

Post 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>
Last edited by joviyach on Tue Nov 29, 2005 1:20 pm, edited 1 time in total.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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 )
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

Post 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
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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?
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

Post by joviyach »

db = "cdcatalog"

table = "cds"

fields

title char(25) unique
genre char(25)
band char(25)
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

Post 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!
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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?)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
Post Reply