Hello, I am having a bit of a problem with my code.
I have to link a php form with a mysql database.
I know it is relativley simple, but I have been looking at my code for hours, and I can't find what is wrong.
I have a sample of my code below:
Any guidence is appreciated.

$name=$_POST['name']; 
$products=$_POST['products'];
$email=$_POST['email']; 
$connect=mysql_connect("localhost","student_user","student");
if (mysqli_connect_error())
{ printf("connect failed: %s \n",mysqli_connect_error());
}else{
} mysql_select_db("Proj_DB");

$sql="INSERT INTO 'customers' VALUES('$user', '$products',' $email')');
$result= mysql_query($sql)
if ($result){
echo("Record added.");
} else{
echo("insert failed");
}

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

@giturman810

I know it is relativley simple, but I have been looking at my code for hours, and I can't find what is wrong.

In future post this in PHP section rather than here because this section is actually about database.

First of all you need to check whether you are connected in the DB.

Here is a link will show you how to do that:

http://www.daniweb.com/web-development/php/code/434480/using-phpmysqli-with-error-checking

Regardin about your code I made some corrections

This is your old code:

$name=$_POST['name'];
$products=$_POST['products'];
$email=$_POST['email'];
$connect=mysql_connect("localhost","student_user","student");
if (mysqli_connect_error())
{ printf("connect failed: %s \n",mysqli_connect_error());
}else{
} mysql_select_db("Proj_DB");
$sql="INSERT INTO 'customers' VALUES('$user', '$products',' $email')');
$result= mysql_query($sql)
if ($result){
echo("Record added.");
} else{
echo("insert failed");
}

This is the correction I made:

$name=$_POST['name'];
$products=$_POST['products'];
$email=$_POST['email'];
$connect=mysqli_connect('localhost', 'myuser', 'mypassword', 'mydatabase');
if (mysqli_connect_error()){
printf("connect failed: %s \n",mysqli_connect_error());
}
mysqli_select_db($connect);
$sql="INSERT INTO 'customers' VALUES('$user', '$products', '$email'))";
$result= mysqli_query($connect, $sql);
if ($result){
echo "Record added";
} else{
echo "insert failed";
}

If there is an error post it here.

Ok. It loaded, but the if statements don't seem to be functioning. Any ideas?
Also, I checkd to see if it filled in data, but all i got was Empty set.
I used the SELECT* FROM command.
Was that the right command to show table entries? Sorry, I'm still learning.

Member Avatar for LastMitch

Are you connected to database>?

Ok. It loaded, but the if statements don't seem to be functioning. Any ideas?

Which one?

I used the SELECT* FROM command.

I don't see it? Where is it?

I only see this:

$sql="INSERT INTO 'customers' VALUES('$user', '$products', '$email'))";

Was that the right command to show table entries? Sorry, I'm still learning.

I think you need to learn how mysqli and php works

I am connected to the Proj_DB database. I used Select*From in mysql to get an output of the table, not in my code.

Member Avatar for LastMitch

I am connected to the Proj_DB database. I used Select*From in mysql to get an output of the table, not in my code.

So you are trying to used SELECT from PHPMYADMIN not from the query.

To SELECT you need to select a Column

It should be like this:

SELECT columnname FROM customers;

What is your column name?

If you don't know just post the table that you create which is customers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.