Error when inserting into table - php

I get this: Catchable fatal error: Object of class mysqli could not be converted to string error when i want to insert data into my games table.
On my games.php i have a form that sends data to games_add.php and error occurs on line 18 in games_add.php.
(i know a lot of code) game.php code:
<body>
<form action="games_add.php" method="post">
Game name: <input type="text" name="game_name" placeholder="Enter first name ..." required="required" /><br />
Relese date: <input type="date" name="relese_date" placeholder="Enter relese date ..." required="required" /><br />
Introduction: <input type="text" name="introduction" placeholder="Enter introduction ..." required="required" /><br />
Description: <input type="text" name="description" placeholder="Enter description ..." required="required" /><br />
Original price: <input type="number" name="rating" placeholder="Enter original price ..." required="required" /><br />
Developer: <select name="developer_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM developers";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Publisher: <select name="publisher_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM publishers";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Categories: <select name="categories_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Platform: <select name="platform1_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM platforms";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
<input type="submit" value="Insert" />
<input type="reset" value="Cancle" />
games_add.php code:
<?php
include_once 'connection.php';
$game_name = $_POST['game_name'];
$relese_date = $_POST['relese_date'];
$introduction = $_POST['introduction'];
$description = $_POST['description'];
$rating = $_POST['rating'];
$developer_id = $_POST['developer_id'];
$publisher_id = $_POST['publisher_id'];
$categories_id = $_POST['categories_id'];
$platform1_id = $_POST['platform1_id'];
$avrage_score = 33;
$query = sprintf("INSERT INTO games (game_name, relese_date, introduction, description, rating, developer_id, publisher_id, categories_id, platform1_id, avrage_score )
VALUES ('%s','$relese_date','%s','%s','$rating','%s','%s','%s','%s','$avrage_score') or die(mysqli_error($link)); ",
mysqli_real_escape_string($link, $game_name),
mysqli_real_escape_string($link, $introduction),
mysqli_real_escape_string($link, $description),
mysqli_real_escape_string($link, $developer_id),
mysqli_real_escape_string($link, $publisher_id),
mysqli_real_escape_string($link, $categories_id),
mysqli_real_escape_string($link, $platform1_id));
mysqli_query($link, $query);
header('Location: games.php');
?>
Picture of my database if it helps.

Are you sure you want "or die(mysqli_error($link))" to be part of your SQL and not part of your PHP?

Related

Pre-Populate Form with Dropdown Menu in PHP/MySQL?

I am creating a simple CRUD application that will be used as a blog. For the edit page, I want to have a dropdown menu with the blog titles of each post. When an option/blog post is selected, I want it to populate the "Title" and "Message" fields, so it can then be edited and saved to the database.
I got it to retrieve the titles of the blog posts, but I am struggling to make it populate the "Title" and "Message" fields so it can be edited when the option is selected.
I have 4 rows in my database: row[0] is the title, row[1] is the message, row[2] is the timestamp and row[3] is the ID.
Thanks guys. I appreciate it.
<form action="edit.php" id="myform" method="post" autocomplete=off>
<input type="hidden" name="action" value="show">
<p><label>Entry:</label><select name="blog">
<?php
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$chosen = $row['bid'];
}
if (isset($_GET['blog'])) {
$id = $_GET['blog'];
$result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
$row = mysqli_fetch_array($result);
}
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$id = $row['bid'];
$title = $row['title'];
$selected = '';
if ($id == $chosen) {
$selected = "selected='selected'";
}
echo "<option value='$id' $selected>$title</option>\n";
}
?>
</select></p>
<p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
<p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
<p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>
<br><p><input type="submit" name="submit" value="Submit"></p>
</form>
I have managed to somewhat figure out the answer on my own. It's probably not the most ideal way of doing it, but for anyone else potentially stuck on this - here is my code:
The only issue I'm having now is figuring out how to keep my option selected.
<form action="edit.php" id="myform" method="post" autocomplete=off>
<input type="hidden" name="action" value="show">
<p><label>Entry:</label><select name="blog" onchange="window.location.href = blog.options[selectedIndex].value">
<option selected disabled>Select One</option>
<?php
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$id = $row['bid'];
$title = $row['title'];
echo "<option value='edit.php?edit=$row[bid]'>$title</option>\n";
}
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
$row = mysqli_fetch_array($result);
}
?>
</select></p>
<p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
<p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
<p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>
<br><p><input type="submit" name="submit" value="Submit"></p>
</form>

Only updating half the data to MySQL table

The code below is for updating data in a MySQL table. It was written by pulling all the data from one query but I have tried to adapt it to pull data from two queries to improve the ordering. Now only some of the records update when the submit button is clicked and I'm not sure how to fix it.
The original code was:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
if($info!=""){$query .= ",info='$info'";}
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "select * from tpf_rides where park_id=".$id;
$res = mysql_query($qry);
$no = mysql_num_rows($res);
$x = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
if($row['type']!="Roller Coaster"){
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
}
echo '<tr><td colspan="2"><hr></td></tr>';
} ?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
and the new code where I altered the queries is here:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="*****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
$query .= ",info='$info'";
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type LIKE '%Roller Coaster%' ORDER BY name ASC";
$res = mysql_query($qry);
$qry2 = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type NOT LIKE '%Roller Coaster%' ORDER BY name ASC";
$res2 = mysql_query($qry2);
$qry3 = "SELECT * FROM tpf_rides WHERE park_id = $id";
$res3 = mysql_query($qry2);
$no = mysql_num_rows($res3);
$x = 0;
$xx = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
while($row2 = mysql_fetch_array($res2)){ $xx++;
echo '<input type="hidden" value="'.$row2['ride_id'].'" name="ride_id'.$xx.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$xx.'" value="'.$row2['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$xx.'" value="'.$row2['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$xx.'" value="'.$row2['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$xx.'" value="'.$row2['info'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
After testing what is not getting amended, it is data from both the LIKE and NOT LIKE queries being skipped so perhaps a record count problem?
Any ideas what I have done wrong?
Are you sure your column type has value 'Roller Coaster' or not?
Sorry to post as an answer as I have no rights no comment.

PHP Form update will not update

I have written an Edit part of my Form but somehow it will not Update the edited Fields. The Code does not give any Errors but it will not update?!
If possible could somebody take a look please?
<?php
include "connect.php";//database connection
if (isset($_GET["id"])) {
$id = intval($_GET["id"]);
if (isset($_POST["edited"]))
{
$update = "UPDATE traumprojekt SET";
$update .= sprintf("quantityProduct='%s', " , mysql_real_escape_string($_POST["quantityProduct"]));
$update .= sprintf("titleProduct='%s', " , mysql_real_escape_string($_POST["titleProduct"]));
$update .= sprintf("informationProduct='%s'", mysql_real_escape_string($_POST["informationProduct"]));
$update .= "WHERE id = '$id'";
mysql_query($update);
}
$sql = "SELECT * FROM `traumprojekt` WHERE id=$id";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
?>
<form method="post" action="edit_form.php?id=<?php echo $row["id"] ?>">
ID: <?php echo $row["id"] ?><br />
Quantity: <input type="text" name="quantityProduct" value="<?php echo $row["quantityProduct"] ?>"><br />
Product Title: <input type="text" name="titleProduct" value="<?php echo $row["titleProduct"] ?>"><br />
Product Information: <input type="text" name="informationProduct" value="<?php echo $row["informationProduct"] ?>"><br />
<input type="submit" name="submit" value="Update"><br />
<input type="hidden" name="edited" value="1">
</form>
<?php
}
}
?>

updating mysql data using php with a drop down option

I am trying to update my products table using a product form but it is throwing an error for an undefined variable here is my code for the productform.php
<?php
$ProductID= $_GET['ProductID'];
//Connect and select a database
mysql_connect ("localhost", "root", "");
mysql_select_db("supplierdetails");
{
$result = mysql_query("SELECT * FROM products WHERE ProductID=$ProductID");
while($row = mysql_fetch_array($result))
{
$ProductID= $_GET['ProductID'];
$ProdDesc = $row['ProdDesc'];
$SupplierID = $row['SupplierID'];
$Location = $row['Location'];
$Cost = $row['Cost'];
$Status = $row['Status'];
$MRL = $row['MRL'];
$ProductID = $row['ProductID'];
}
?>
//form
<form action="Edit_Prod.php" method="post">
<input type="hidden" name="ProductID" value="<?php echo $ProductID; ?>"/>
<label>Product Description:
<span class="small">Please enter a description for the product </span>
</label>
<input type="text" name="ProdDesc" value="<?php echo $ProdDesc ;?>" />
<label>Supplier ID
<span class="small">Please enter the Supplier ID</span>
</label>
<input type="text" name="SupplierID" value="<?php echo $SupplierID ;?>" />
<label>Bay Location:
<span class="small">Please select the bay location for this product </span>
</label>
<select name="Location" value="<?php echo $Location ;?>" />
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
<option>B1</option>
<option>B2</option>
<option>B3</option>
<option>B4</option>
<option>B5</option>
<option>C1</option>
<option>C2</option>
<option>C3</option>
<option>C4</option>
<option>C5</option>
<option>D1</option>
<option>D2</option>
<option>D3</option>
<option>D4</option>
<option>D5</option>
<option>E1</option>
<option>E2</option>
<option>E3</option>
<option>E4</option>
<option>E5</option>
</select>
<label>Product Cost:
<span class="small">Please enter a new cost for the product (per roll) </span>
</label>
<input type="text" name="Cost" value="<?php echo $Cost ;?>" />
<label>Status:
<span class="small">Please select a status for the product </span>
</label>
<select name="Status" value="<?php echo $Status ;?>" />
<option>Live</option>
<option>Mature</option>
<option>Obsolete</option>
</select>
<label>MRL
<span class="small">Please enter a minimum re-order level for this product </span>
</label>
<input type="text" name="MRL" value="<?php echo $MRL ;?>" />
<input type="submit" value= "Edit Product Details"/>
Undefined variable all lines, do i have to do anything differently if using drop down areas*
*Edit_Prod.php*
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("supplierdetails");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Run a query
$ProductID = $_POST['ProductID'];
$result = mysql_query ("SELECT * FROM products WHERE Productid= '".$Productid."'") or die(mysql_error());
$row = mysql_fetch_array($result);
$ProdDesc = $_POST['ProdDesc'];
$SupplierID = $_POST['SupplierID'];
$Location = $_POST['Location'];
$Cost = $_POST['Cost'];
$Status = $_POST['Status'];
$MRL = $_POST['MRL'];
$Productid=$row['Productid'];
$query="UPDATE products SET ProdDesc='".$ProdDesc."', SupplierID='".$SupplierID."', Location='".$Location."', Cost='".$Cost."', Status='".$Status."', MRL='".$MRL."' WHERE ProductID='".$ProductID."'";
$result = mysql_query($query);
//Check whether the query was successful or not
if($result)
{
echo "Products Updated";
header ("Location: Products.php");
}
else
{
die ("Query failed");
}
?>
i think $row vairable is used to get values from database which you have missed
$productID= $_GET['productid'];
$records =mysql_query(" select * from tableName where productid=$productID");
$row=mysql_fetch_array($records);
then your code will work
$supplId= $row['supplId'];
...
Answering to your question "do i have to do anything differently if using drop down areas", of course you have to change it. Change:
<select name="Location" value="<?php echo $Location ;?>" />
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
.
.
. . .. .
To:
<select name="Location" />
<option value="<?=$Location;?>" selected="selected" ></option>
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
like that. I think you got the idea. You have to get value as an option under select. Try it. Good luck.

Sending id through select menu using PHP

here is my problem, I want to send id number through select menu using PHP.
Here is the code:
<form name="update" method="post" action="ex_update.php?id=<?php echo ((int)$_POST['get_id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
I have tried but when I submit the id in the URL equals to zero. how can I send id to the URL??
here is the ex_update.php >>>
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_GET["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id=={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>
Thanks in advance
You can use the form GET method
<form name="update" method="GET" action="ex_update.php">
You can access that select box value using $_GET['get_id'] in ex_update.php
Here is your First Page
Note action of form...
<form name="update" method="post" action="ex_update.php">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
And here you can find ex_update.php. Note: $id = (int)$_POST["get_id"];
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_POST["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>

Categories