Solved

MySQL query is getting denied

Posted on 2012-04-02
4
255 Views
Last Modified: 2012-06-21
My query is not being successfully added to my database table. Can someone please tell me where I am going wrong in my code?

<HTML>

<HEAD><TITLE>Florida Pet Clinics Inc.</TITLE>

</HEAD>

<BODY>

<H1 ALIGN="CENTER">Florida Pet Clinics Inc. </H1>

<?php
define("MYSQLUSER", "???");
define("MYSQLPASS", "???");
define("HOSTNAME", "???");

$connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS);
if ($connection->connect_error) {
	die('Connect Error: ' . $connection->connect_error);
} else {
	echo 'Successful connection to MySQL<br>';

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$phone = $_POST['phone'];

$name = $last_name . ', ' . $first_name;

echo $name . '<br>';
echo $address . '<br>';
echo $phone . '<br>';


$query = "INSERT INTO 'Account' VALUES "
	. " ('123', '$name', '$address', '$phone')";

if (!$result = $connection->query($query)) {
	echo 'Unable to add rows';
}
else {
	echo 'Row successfully added';
}

}

?>

</BODY>

</HTML>

Open in new window


My variables are being output correctly via the echo methods I used to test.
0
Comment
Question by:InquisitiveProgrammer
  • 2
4 Comments
 
LVL 9

Expert Comment

by:damerval
Comment Utility
Hi,
I may be wrong, but it seems you are using single quotes (' ' ) instead of the MySQL database object quote character  (` `) which is right on top of the TAB key on US keyboards.
Your insert should read:

INSERT INTO `Account` VALUES "
      . " ('123', '$name', '$address', '$phone')
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
Comment Utility
I tried that and still no change.
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
Comment Utility
This is the output I get from my program:

Successful connection to MySQL
Lefler, Jay
1145 Running
8137587733
INSERT INTO `Account` VALUES ('123', 'Lefler, Jay', '1145 Running', '8137587733')
Unable to add rows
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
You haven't selected a database.  The fourth parameter should be the name of your database.  And when you are still trying to get it working, you should not be putting the '@' in there to suppress error messages that would tell if something was wrong.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now