Page 1 of 1

Using auto increment in PHP

Posted: Fri Mar 04, 2011 9:55 am
by blacksmit049
I'm just a beginner, when I search for auto increment in PHP, i did what I've search. But it doesn't insert very well in the database. The if else statement doesn't show up anything so I can't see the error.

Does the result carry a value, after $result=$db->query($query);?

Thanks for the help.

Code: Select all

$cellphone_nr=$_POST['cellphone_nr'];
$amount=$_POST['amount'];
$month=$_POST['month'];
$day=$_POST['day'];
$year=$_POST['year'];

$date=$year."-".$month."-".$day;


@ $db = mysqli('localhost', 'root','12345', 'load_business');

if (mysqli_connect_errno()){
	echo 'Error: Could not connect to the database.';
	exit;
}

$query = "insert into globe (cellphone_nr, amount, date) values
		('".$cellphone_nr."','".$amount."','".$date."')";

$result=$db->query($query);

if($result){
	echo $db->affected_rows." book inserted into database."; 
	echo $cellphone_nr."<br>";
	echo $amount."<br>";
	echo $date;
	}
else{
	echo "An error has occurred. The item was not added.";
}

$db->close();

Re: Using auto increment in PHP

Posted: Fri Mar 04, 2011 10:29 am
by social_experiment

Code: Select all

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// try this
$db = new mysqli('localhost', 'root','12345', 'load_business');
?>
To use the mysqli object you have instantiate it. Only add the '@' once your script is working correctly because currently you are supressing errors that could give information about the problem.
Hth