Using auto increment in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blacksmit049
Forum Newbie
Posts: 2
Joined: Sat Jan 29, 2011 4:58 am

Using auto increment in PHP

Post 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();
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Using auto increment in PHP

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply