Reputation: 13
I am using php and mysql to add data to a database. When I add data to a form and submit the data I get a message saying the data has been successfully added. However when I view the database on phpmyadmin all I see is a blank row with nothing but the primary key. The database is storing cricket players that the user has entered. UPDATE: I put the colons in just like everyone said and now the IF statement comes back saying Could not add data. So now it is not adding anything at all. Here is my code:
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$dob=$_POST['dob'];
$teamrole=$_POST['teamrole'];
$testruns=$_POST['testruns'];
$testwickets=$_POST['testwickets'];
$odiruns=$_POST['odiruns'];
$odiwickets=$_POST['odiwickets'];
$truns=$_POST['truns'];
$twickets=$_POST['twickets'];
$searchteam=$_POST['searchteam'];
$query="INSERT INTO player VALUES (NULL, :First_Name, :Last_Name, :Date_of_Birth, :Team_Role, :Test_Runs, :Test_Wickets, :ODI_Runs, :ODI_Wickets, :t20_Runs, :t20_Wickets, :TeamID) ";
$stmt=$conn->prepare($query);
$stmt->bindValue(':First_Name', $fname);
$stmt->bindValue(':Last_Name', $lname);
$stmt->bindValue(':Date_Of_Birth', $dob);
$stmt->bindValue(':Team_Role', $teamrole);
$stmt->bindValue(':Test_Runs', $testruns);
$stmt->bindValue(':Test_Wickets', $testwickets);
$stmt->bindValue(':ODI_Runs', $odiruns);
$stmt->bindValue(':ODI_Wickets', $odiwickets);
$stmt->bindValue(':t20_Runs', $truns);
$stmt->bindValue(':t20_Wickets', $twickets);
$stmt->bindValue(':TeamID', $searchteam);
$affected_rows = $stmt->execute();
if($affected_rows==1)
{
echo "Successfully added the details for ".$fname;
}
else
{
echo "Could not add data for ".$fname;
}
Upvotes: 0
Views: 476
Reputation: 181047
Your query isn't quite correctly constructed, you'll need to add the parameters using :
, something like this;
$query="INSERT INTO `player` (`First_Name`, `Last_Name`, `Date_of_Birth`,
`Team_Role`, `Test_Runs`, `Test_Wickets`, `ODI_Runs`, `ODI_Wickets`,
`t20_Runs`, `t20_Wickets`, `TeamID`)
VALUES (:First_Name, :Last_Name, :Date_of_Birth, :Team_Role,
:Test_Runs, :Test_Wickets, :ODI_Runs, :ODI_Wickets,
:t20_Runs, :t20_Wickets, :TeamID)";
Note that since it's automatically generated you should leave out the value for the PlayerID primary key, which you can only do if you're listing the columns where you actually want to insert values.
As a secondary benefit, if you ever decide to add a field to your table, listing the fields involved in the query like this will prevent the insert from failing because of missing columns.
Upvotes: 1
Reputation: 6592
Your sql is incorrect. You're binding on parameters that start with colons, but your sql statement doesn't include them
$query="INSERT INTO `player` VALUES (NULL, :First_Name, :Last_Name ...
Back ticks are also MySQL's field delimiters.
Upvotes: 1
Reputation: 24425
Your binding names are wrong in your query - remove the ` characters and use semi colons (as per your binding variables). Also, when you're inserting a new row, you don't specify the primary key as it should be generated automatically.
$query = "INSERT INTO `player` VALUES (null, :First_Name, :Last_Name, :Date_of_Birth, :Team_Role, :Test_Runs, :Test_Wickets, :ODI_Runs, :ODI_Wickets, :t20_Runs, :t20_Wickets, :TeamID) ";
Docs: http://php.net/manual/en/pdostatement.bindparam.php
Upvotes: 2