INSERT not INSERTING no matter what!

Hi all - VERY desperate as 3 days I’ve been searching for the error without joy and I’m about to give up aaarrrrgghhh! I have tried and tried but the following code refuses to INSERT into the database unless I run an SQL COMMAND directly to the database even though another command which is almost identical DOES INSERT into the same database but different table - I have even rebuilt the table but nothing… am I missing something? thanks in advance guys x

http://www.codejaw.com/2id05mk

Tr inserting the following to show any errors

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

// check $result
var_dump( $result );

// check errors
echo mysqli_error();

You’re checking for “isset($_POST[‘submit’])” in the processing code, but you’re not setting that value anywhere. In my small test page I had to use

<button name="submit">Submit</button>

Thanks guys - updated code to include both replies but madly and insanely it still isn’t inserting my data - it’s almost as if it’s inserting the data elsewhere!!!

Updated code

http://www.codejaw.com/wc4bb

You might need to set the error_reporting at the start of the script, not after all the good stuff has already happened - I am not sure whether a pre-pass will see this and set it for the whole script, it’s not something I’ve used much.

Other than that, add some echo() statements into the php code so you can see how far it gets through and where it’s stopping, if it’s stopping somewhere. What actually happens when you press the button - does it run the php code but not insert the data, or not run the code? Does your database allow you to insert more than one row with cadID=0? I’m unsure about it all seeming to be within a function definition - not saying that won’t work, just not a layout I’m familiar with.

Might be an idea to post your code here rather than just a link to it, I am sure many won’t click on blind links.

It seems to run the code but not insert the data - I put the error reporting to the top of the function and it reports

"Notice: Undefined variable: result in
/home/sites/worthingads.co.uk/public_html/functions.php on line 136
NULL

Warning: mysqli_error() expects exactly 1 parameter, 0 given in
/home/sites/worthingads.co.uk/public_html/functions.php on line 139"

Quite honestly I thought I’d left out a ; somewhere or something but this exact code dosen’t even work if I cut the inserts to say CadID only!

So if you add this line

if (isset($_POST['submit'])) {
echo "** Processing form posting **";    // add this line here

does it say it’s processing the post?

I haven’t used mysqli myself, does the undefined variable $result imply that the return from your query was null and therefore it didn’t execute? What’s the result of echoing $query?

Also you use the NOW() function in your form variable processing code - is that correct? A brief search suggests that’s an sql function and that you’d need to use date() or datetime() to set those values. Would having invalid date/time values in those fields cause a problem from your table definition?

(ETA my edit to add the last para above cross with Starlion by the look of it)

*Standard response: I’d advise looking into prepared statements rather than fixed queries.

That said, Let’s address the error messages you’re getting.

I assume the “NULL” is your var_dump. That then is indicating your line

$result = mysqli_query($dbc, $query) or die('MySQL Error: ' . mysqli_error($dbc));

Did not execute. (Note that this should still be a single line, even if it may work otherwise)

The second error is because you didnt put your $dbc variable into the second mysqli_error call.

Now general statements:
I wouldnt check against $_POST[‘submit’], but by something like $_POST[‘email_address’].
There is no function “NOW()” in PHP. So i dont know what that function is doing.
You’re echoing out your query at one point in the code. I dont see that in your report. Is it there? Does it look right?

All changes made - the WEIRDEST thing about this is that the other function I have is working but more simple as in just a few fields BUT say for example I put the following: -

$query = "INSERT INTO classifieds (cadID) VALUES (0) "; it STILL doesn’t INSERT anything! the database table remains empty! PLUS where I have put echo $query returns nothing…

Does the line that droop told you to insert show up?

Nope :confused:

I changed it

** Processing form posting **
Notice: Undefined index: username in
/home/sites/worthingads.co.uk/public_html/functions.php on line 202

Notice: Undefined variable: POST in
/home/sites/worthingads.co.uk/public_html/functions.php on line 203
INSERT INTO classifieds (cadID, cadViewCount, cadCatID, cadCreated,
cadAvailable, cadTerminate, cadQuantity, cadPrice, cadType, cadLocation,
cadPhone, cadTitle, cadCondition, cadSourceIP, cadPurchaseNow,
cadDescription, cadLive, postedBy) VALUES (0, 0, 0, NOW(), NOW(), NOW(),
‘4’, ‘3.00’, ‘For sale’, ‘Worthing’, ‘01903265451’, ‘bbbbbbbbbb’,
‘New’, ‘80.7.19.217’, ‘Y’, ‘bbbbbbbbbb’, ‘Y’,‘’)

IT’S WORKED!! I changed it to

if (isset($_POST[‘cadDescription’])) {

it has all gone into the database!

Thank you guys :smile:

Those error messages relate to

<input type="hidden" name ="postedBy" value="<?php echo $row['username']; ?>">
... 
$postedBy = $_POST['username'];             // you don't call it 'username', it's 'postedBy' in the form
$email_address = $POST['email_address'];    // missing underline in this $_POST reference

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.