Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL query is getting denied

Posted on 2012-04-02
4
Medium Priority
?
265 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:Philippe Damerval
ID: 37797384
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
ID: 37797401
I tried that and still no change.
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 37797430
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 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 37797451
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

885 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