Author Topic: Insert query problem?  (Read 482 times)

0 Members and 1 Guest are viewing this topic.

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Insert query problem?
« on: July 03, 2009, 12:33:54 AM »
Code: [Select]
$query =  "INSERT INTO Test VALUES (5," . $INFO1 . "," $INFO2 . "," . $INFO3 . "," . $INFO4 . "," . $INFO5)
Any idea why i get this error when adding this query? (the error comes after the connection has been made so the file does connect to the database, there must be though a problem with how its formatted?

Offline dezkit

  • Devotee
  • Posts: 1,223
  • Gender: Male
  • Professor Badass
    • View Profile
Re: Insert query problem?
« Reply #1 on: July 03, 2009, 12:36:03 AM »
$query =  "INSERT INTO Test VALUES (5," $INFO1 "," $INFO2 "," $INFO3 "," $INFO4 "," $INFO5 ")";
why hello thar

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Insert query problem?
« Reply #2 on: July 03, 2009, 08:58:11 AM »
Stilll get a error.

Heres the full code.
Code: [Select]
$mysql = mysql_connect("127.0.0.1", "root", "")
$query =  mysql_query("INSERT INTO Test VALUES (5," . $INFO1 . "," $INFO2 . "," . $INFO3 . "," . $INFO4 . "," . $INFO5 . ")");

The error comes up on the $query line.

Offline Mark Baker

  • Addict
  • Posts: 1,586
  • Gender: Male
    • View Profile
Re: Insert query problem?
« Reply #3 on: July 03, 2009, 10:12:29 AM »
If any of the $INFO values you're inserting into the table are strings, then they need to be quoted
9 out of 10 PHP problems can be resolved by setting
Code: (php) [Select]
error_reporting(E_ALL);
ini_set('display_errors', 1);
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend
Development Projects: PHPExcel and PHPPowerPoint

Offline gevans

  • Addict
  • Posts: 2,649
  • Gender: Male
  • don't shoot the messanger
    • View Profile
    • Guernsey Web Design
Re: Insert query problem?
« Reply #4 on: July 03, 2009, 10:20:24 AM »
What's the error?

and do you select a database?

http://www.php.net/manual/en/function.mysql-select-db.php
« Last Edit: July 03, 2009, 10:21:25 AM by gevans »
I like starting my code with session_start();

Code: [Select]
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
Son of a biscuit eating bulldog
Use [code][/code] tags!!

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Insert query problem?
« Reply #5 on: July 03, 2009, 01:31:16 PM »
How come it works with this though?

Code: [Select]
mysql_query("INSERT INTO `Test`.`user` (`id`, `name`, `password`, `ip`, `date`, `registered`) VALUES (NULL,                   'test', 'test', 'test', 'test', 'test 'test', 'test')", $c);
But if i adjust this even a little bit it doesn't work. why does this above example work yet this doesn't?

mysql_query("INSERT INTO" . $webdbname . "`user` (`id`, `name`, `password`, `ip`, `date`, `registered`) VALUES (NULL," .                               $sname . "," . $spassword . "," . $sip . "," . $date)", $c);[/code]
« Last Edit: July 03, 2009, 01:32:23 PM by axx5 »

Offline gevans

  • Addict
  • Posts: 2,649
  • Gender: Male
  • don't shoot the messanger
    • View Profile
    • Guernsey Web Design
Re: Insert query problem?
« Reply #6 on: July 03, 2009, 01:48:56 PM »
I like starting my code with session_start();

Code: [Select]
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
Son of a biscuit eating bulldog
Use [code][/code] tags!!

Offline gevans

  • Addict
  • Posts: 2,649
  • Gender: Male
  • don't shoot the messanger
    • View Profile
    • Guernsey Web Design
Re: Insert query problem?
« Reply #7 on: July 03, 2009, 01:54:20 PM »
You're not putting quotes around the value strings;

Code: [Select]
<?php
$mysql 
mysql_connect("127.0.0.1""root""")
$query =  mysql_query("INSERT INTO Test VALUES (5, '$INFO1', '$INFO2', '$INFO3', '$INFO4', '$INFO5')");
I like starting my code with session_start();

Code: [Select]
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
Son of a biscuit eating bulldog
Use [code][/code] tags!!

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Insert query problem?
« Reply #8 on: July 03, 2009, 02:40:51 PM »
Man this is driving me crazy. Can someone just post a full example with all the code, connect, select, add? I can't get to work when $ strings are added

Offline gevans

  • Addict
  • Posts: 2,649
  • Gender: Male
  • don't shoot the messanger
    • View Profile
    • Guernsey Web Design
Re: Insert query problem?
« Reply #9 on: July 03, 2009, 03:02:12 PM »
Again, what error are you getting, and try my example
I like starting my code with session_start();

Code: [Select]
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
Son of a biscuit eating bulldog
Use [code][/code] tags!!

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Insert query problem?
« Reply #10 on: July 03, 2009, 04:07:46 PM »
Again, what error are you getting, and try my example
I am not getting a error any more, it just doesn't send the data to the database when $strings are inserted. Which is why i asked if you could supply a full demonstration doing it.

Offline Zane

  • Global Moderator
  • Fanatic
  • *
  • Posts: 3,895
  • Gender: Male
    • View Profile
Re: Insert query problem?
« Reply #11 on: July 03, 2009, 04:14:58 PM »
you are missing a semicolon here
$mysql mysql_connect("127.0.0.1""root""")

Want to thank me?  Contribute to my PayPal piggy-bank

Offline axx5Topic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Insert query problem?
« Reply #12 on: July 03, 2009, 04:42:53 PM »
You're not putting quotes around the value strings;

Code: [Select]
<?php
$mysql 
mysql_connect("127.0.0.1""root""")
$query =  mysql_query("INSERT INTO Test VALUES (5, '$INFO1', '$INFO2', '$INFO3', '$INFO4', '$INFO5')");
That fixes it but then if i add quotes on
'date("Y:m:d")'
it doesn't work, ideas?

Offline mattal999

  • Devotee
  • Posts: 693
    • View Profile
    • Luke Zbihlyj
Re: Insert query problem?
« Reply #13 on: July 03, 2009, 05:14:43 PM »
It does that because you don't excape the double quotes. Replace

'date("Y:m:d")'

With

'".date("Y:m:d")."'
Check out my portfolio, which entales all of my little projects and client work. Not interested in that? Well then take a glance at my blog and see if anything tickles your fancy!