db error

Hello, im new to php want to know if any of you guy can help me with this problem. Im trying to add data to the database through a form. Here is the code for the form.

Date

Event

Here is the code to process form

<?php if (isset($_POST['submit'])) { include('event.php'); $date = $_POST['date']; $event = $_POST['event']; $sqlinsert = "INSERT INTO church (date, event) VALUES ('$date', '$event')"; if (!mysqli_query($dbcon, $sqlinsert)) { die('error inserting new event'); $newrecord = "1 record added to the database"; } // end of me nested if statement } // end of the main if ?>

The include with the db connection

<?php define('DB_NAME' , 'myjesus2_xxxx'); define('DB_USER' , 'myjesus2_xxxx'); define('DB_PASSWORD' , 'xxxxxxxx'); define('DB_HOST' , 'localhost'); $dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$dbcon) { die('error connecting to database'); } echo 'you have connected sucessfully'; ?>

I created 3 tables in mysql, ID, which is primary and auto INCREMENT, then i added date using date for the type and nulled is checked, the third is event and its type is varchar and its also nulled.

but i keep getting “you have connected sucessfullyerror inserting new event”

i can figure out problem… help please!!!

First, did you “CREATE” the database “church” table? You have created your database as you connected to it.
But, once you create a database, next you have to create a table in it. If you didn’t, that is your next step.

If you already created the table, then you have to print the error that you are receiving from the query.
Lastly, if you did create the table and if you place one of the two fields in it as indexed or unique, then,
you will get an error if you enter the same data twice. So, fill us in on your table info…

yes i created the table church, and the fields are , ID, date and event

this is what it display when i execute it

you have connected sucessfullyerror inserting new event

Is the ID field auto-increment?

Oh, wait, I just realized… You are using MySQLi code, but, are doing your queries as MySQL !!!
They are totally different…

A MySQLi INSERT is something like this:

$sqlinsert = “INSERT INTO church (date, event) VALUES (’$date’, ‘$event’)”;
$mysqli->query($sqlinsert);
NOT
mysqli_query($dbcon, $sqlinsert);

Hope that helps!

The ID is set to auto increment. and i found the script at “http://www.youtube.com/watch?v=qy2-d8uAn2k”. in the dbcon part it has this line

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

and for the form processing it has

$sqlinsert = “INSERT INTO church (date, event) VALUES (’$date’, ‘$event’)”;

im not sure what you think i should change

try changing this:

mysqli_query($dbcon, $sqlinsert);

to

$mysqli->query($sqlinsert);mysqli_query($dbcon, $sqlinsert);

no didnt work

Sorry, fast typing… Getting tired!

try changing this:

mysqli_query($dbcon, $sqlinsert);

to

$mysqli->query($sqlinsert);

What error are you getting?

it gives me error in dreamweaver syntax error

<?php if (isset($_POST['submit'])) { include('event.php'); $date = $_POST['date']; $event = $_POST['event']; $sqlinsert = "INSERT INTO church (date, event) VALUES ('$date', '$event')"; if $mysqli->query($sqlinsert); { die('error inserting new event'); $newrecord = "1 record added to the database"; } // end of me nested if statement } // end of the main if ?>

That’s not the code I gave you, but, I can fix it:

Try this (for what you just posted…):

[php]

<?php if (isset($_POST['submit'])) { include('event.php'); $date = $_POST['date']; $event = $_POST['event']; $sqlinsert = "INSERT INTO church (date, event) VALUES ('$date', '$event')"; if ($mysqli->query($sqlinsert)) { $newrecord = "1 record added to the database"; }else{ die('error inserting new event'); } // end of me nested if statement } // end of the main if ?>

[/php]

ok thanks, it give another error now

Fatal error: Call to a member function query() on a non-object in /home/myjesus2/public_html/uevent.php on line 9

Well, I just search in on Google and it appears you are missing your database name in the connection string!

This was their sample line:
mysqli(‘hostname’,‘username’,‘password’,‘database’);

You have the first three in yours, but, left out the database name. (4th argument)

Try adding it in… I am off to bed soon, but, will hang for a few minutes…

<?php $host="localhost"; $username="myjesus2_test"; $password="test1234"; $database="myjesus2_data"; $table="church"; mysql_connect("$host", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $mysql="INSERT INTO $table(date, event) VALUES ('$_POST[date]','$_POST[event]')"; if(!mysql_query($mysql)) die(mysql_error()); echo"Data Inserted"; header('location: uevent2.php'); mysql_close(); ?>

ok i got i working like this, but im trying to redirect it back to the events2.php page but i keep getting

Data Inserted
Warning: Cannot modify header information - headers already sent by (output started at /home/myjesus2/public_html/event2.php:18) in /home/myjesus2/public_html/event2.php on line 19

header(‘location: uevent2.php’); - i added that , without it it work fine

Well, this new error is due to a header issue. Let’s explain them a bit…

When you create a HTML page, the first thing it sends to the browser is a header. (Such as a DOCtype or whatever…) Then, it sends the HTML. In most PHP pages, the PHP processes SERVER-SIDE and spits out some HTML code created from databases and echo’s of HTML code. All normal.

Now, PHP can not send a header out to the browser if one is already sent. Only one header will be processed and any other will flag an error. So, if you are planning on using a PHP header to relocate to another page, you can not have any HTML on the page UNLESS it is after the PHP code.

There are many ways to fix that. But, the easiest is to make sure you do not send any HTML to the page before the header. In your code, it displays a message “Data inserted!”… This is HTML. So, your PHP code “POSTS” or “ECHO”'s or PRINTS to the page and the browser makes up a header to handle that display.
I think if you just remove the echo of “Data inserted”, that you should be all set. Unless you have some other HTML code I am not seeing. Hope that explains it… Good luck!

cool thank that worked

Glad to hear it! Congrats! I will mark this solved… Sorry if it took so long, it is hard to debug back and forth sometimes… CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service