Jump to content

[SOLVED] PHP won't write to MySQL database


Stevens3

Recommended Posts

I have an html document which has POST variables PANAS1 - PANAS20, which passes the data to the PANAS.php processor when submitted.  However, when I click submit, the screen pauses a second (as if sending data), then brings up the same HTML page, and when I check the MySQL database no data is entered.  Here is the code for PANAS.php:

 

 

<?php 

$mysqli = mysqli_connect("localhost", "**", "**", "unified"); 

$sql = 'INSERT INTO unified.PANAS (PANAS1, PANAS2, PANAS3, PANAS4, PANAS5, PANAS6, PANAS7, PANAS8, PANAS9, PANAS10, PANAS11, PANAS12, PANAS13, PANAS14, PANAS15, PANAS16, PANAS17, PANAS18, PANAS19, PANAS20) VALUES ("'.{$_POST["PANAS1"].'","'. $_POST["PANAS2"].'","'. $_POST["PANAS3"].'","'. $_POST["PANAS4"].'","'. $_POST["PANAS5"].'","'. $_POST["PANAS6"].'","'. $_POST["PANAS7"].'","'. $_POST["PANAS8"].'","'. $_POST["PANAS9"].'","'. $_POST["PANAS10"].'","'. $_POST["PANAS11"].'","'. $_POST["PANAS12"].'","'. $_POST["PANAS13"].'","'. $_POST["PANAS14"].'","'. $_POST["PANAS15"].'","'. $_POST["PANAS16"].'","'. $_POST["PANAS17"].'","'. $_POST["PANAS18"].'","'. $_POST["PANAS19"].'","'. $_POST["PANAS20"].'"'; 

mysqli_query($mysqli, $sql); 

mysqli_close($mysqli); 
?>

It may not be very efficient code, but I think it should work?  Any help would be greatly appreciated.

Link to comment
Share on other sites

You have this backwards

 

mysqli_query($mysqli, $sql);

 

should be

 

mysqli_query($sql);

 

No, mysqli_query() requires the connection resource and the arguments are in the correct order.

 

For debugging, try...

 

mysqli_query($mysqli, $sql) || die(mysqli_error() . "<br />" . $sql);

 

What do you get?

Link to comment
Share on other sites

Place this at the top of the script.

 

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>

 

ps: Your code isn't terribly innificient, though it has no error handling. Your database design on the other hand leaves alot to be desired. You should look at some tutorials on dtabase normalization.

Link to comment
Share on other sites

Ugh, still returns the same page (the submission form).  Here's the only thing that might be relevant from apache error_log

 

[Mon Dec 03 09:54:15 2007] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico, referer: http://localhost/PANAS.html/

 

I have no idea why it cares about a favicon, but thought I would put this info here is case it was any help in ascertaining the problem.

 

Here is the Apache server_log for one of the submissions.

 

127.0.0.1 - - [03/Dec/2007:10:54:17 -0500] "POST /PANAS.html/PANAS.php HTTP/1.1" 200 9733

 

Perhaps it's looking for the script in the wrong place?

 

 

Sorry for any trouble, but I greatly appreciate the help.

Link to comment
Share on other sites

Are you doing any url rewriting in a .htaccess file? The URL in the lines for the log looks unusual (it would appear you have a folder called PANAS.html or you are typing the / on the end of the URL.)

 

For a form to submit and cause the same page to be displayed when the action="..." parameter is to a different page would mean the form is incorrectly coded or the browser could not figure out the URL to submit to.

 

Take a more basic approach, find out if your form is submitting to the form processing code. Echo out some of the $_POST variables. Once you know the form is working, then debug the mysqli code.

Link to comment
Share on other sites

Ugh, still returns the same page (the submission form).  Here's the only thing that might be relevant from apache error_log

 

[Mon Dec 03 09:54:15 2007] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico, referer: http://localhost/PANAS.html/

 

I have no idea why it cares about a favicon, but thought I would put this info here is case it was any help in ascertaining the problem.

 

Here is the Apache server_log for one of the submissions.

 

127.0.0.1 - - [03/Dec/2007:10:54:17 -0500] "POST /PANAS.html/PANAS.php HTTP/1.1" 200 9733

 

Perhaps it's looking for the script in the wrong place?

 

 

Sorry for any trouble, but I greatly appreciate the help.

 

Favicon is a request from IE for a favorites icon. It always asks for it. If you don't have a favicon on the server, it'll generate a 404 error. Not a problem! Just ignore it...

 

The other log entry you posted is generating a 200, which is a successful transfer, and it is reporting that it successfully transferred 9733 bytes. Ok, so now back to your original problem...

 

PhREEEk

Link to comment
Share on other sites

Ok, some good things are happening.  On the hint that the form itself may not be working, I changed the form action to "/PANAS.php" and now I get output from echoing some post variables instead of the previous page simply reappearing after submitting the form.  However, nothing has been entered into the database yet.  Here is the error message I get:

 

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /usr/local/apache2/htdocs/PANAS.php on line 11

 

INSERT INTO unified.PANAS (PANAS1, PANAS2, PANAS3, PANAS4, PANAS5, PANAS6, PANAS7, PANAS8, PANAS9, PANAS10, PANAS11, PANAS12, PANAS13, PANAS14, PANAS15, PANAS16, PANAS17, PANAS18, PANAS19, PANAS20) VALUES ("3","4","3","4","4","4","4","3","4","4","3","4","4","4","4","3","3","4","4","4"

 

(This is where the numbers are the submitted radio button values - note no right parentheses at the end, I don't know if this means anything)

 

Here is the code (line 11 is the mysqli_query() line):

 

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); 

echo $_POST["PANAS2"];
echo $_POST["PANAS1"];


$mysqli = mysqli_connect("localhost", "root", "", "unified"); 

$sql = 'INSERT INTO unified.PANAS (PANAS1, PANAS2, PANAS3, PANAS4, PANAS5, PANAS6, PANAS7, PANAS8, PANAS9, PANAS10, PANAS11, PANAS12, PANAS13, PANAS14, PANAS15, PANAS16, PANAS17, PANAS18, PANAS19, PANAS20) VALUES ("'. $_POST["PANAS1"].'","'. $_POST["PANAS2"].'","'. $_POST["PANAS3"].'","'. $_POST["PANAS4"].'","'. $_POST["PANAS5"].'","'. $_POST["PANAS6"].'","'. $_POST["PANAS7"].'","'. $_POST["PANAS8"].'","'. $_POST["PANAS9"].'","'. $_POST["PANAS10"].'","'. $_POST["PANAS11"].'","'. $_POST["PANAS12"].'","'. $_POST["PANAS13"].'","'. $_POST["PANAS14"].'","'. $_POST["PANAS15"].'","'. $_POST["PANAS16"].'","'. $_POST["PANAS17"].'","'. $_POST["PANAS18"].'","'. $_POST["PANAS19"].'","'. $_POST["PANAS20"].'"'; 

mysqli_query($mysqli, $sql) || die(mysqli_error() . "<br>" . $sql);

mysqli_close($mysqli); 
?>

 

Thanks again for all help.

Link to comment
Share on other sites

Actually, the current error is because the mysqli_error() syntax is wrong, but because it is trying to execute the or die() statement means that the query failed.

 

Make the following correction to the mysqli_error() -

 

mysqli_query($mysqli, $sql) || die(mysqli_error($mysqli) . "<br>" . $sql);

 

You are correct about a missing ) near the end of the query, that is probably what is causing the query to fail.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.