update php&mysql : undefined index - php

I'm now making reservation system, and something is wrong with updating data. I made table updateMy_ReservationView.php like this.
this is an image of SelectMy_ReservationView.php I have an error with insert more codes because of this editor's error message.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>
And I made updating function file updateMy_ReservationDo.php like below.
include "connection.php";
$reservation_id=$_POST['reservation_id'];
$car_type=$_POST['car_type_id'];
$rent=$_POST['rent'];
$return_time=$_POST['return_time'];
$car_station=$_POST['car_station'];
$sql = "update reservation set car_type='$car_type_id',rent='$rent',
return_time='$return_time' and car_station='$car_station' where
reservation_id=$reservation_id ";
if (mysqli_query($conn, $sql)) {
echo "Reservation is updated successfully<br>";
echo "<p><p><a href=?page=selectMy_reservationView><button type=button>Show
all reservation</button></a>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Then Error message like this happen:
Notice: Undefined index: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 5
Notice: Undefined variable: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 10
Error: update reservation set car_type='',rent='2018-05-31', return_time='2018-06-01' and car_station='Shinchon' where reservation_id=17
Unknown column 'car_type' in 'field list'
What should I modify?

Use isset like following :
$reservation_id = isset($_POST['reservation_id']) ? $_POST['reservation_id'] : '';
$car_type = isset($_POST['car_type_id']) ? $_POST['car_type_id'] : '';
$rent = isset($_POST['rent']) ? $_POST['rent'] : '';
$return_time = isset($_POST['return_time']) ? $_POST['return_time'] : '';
$car_station = isset($_POST['car_station']) ? $_POST['car_station'] : '';
and your defined variable is $car_type but you used in your sql query $car_type_id
use :
$car_type
and make sure car_type field exists in your table

Change the name of car_type to car_type_id.The error is due to you are sending car_type and accessing car_type_id in updateMy_ReservationDo.php.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type_id">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>

Related

object of class mysqli _connect could not be converted to string in line 135

when i run the code it give error of object of class mysqli_connect could not be converted to string in line 135. please some one provide me the solution of this problem.
i tried searching for solution but no one worked for me. bellow is the code showing error.
$row = mysqli_connect("$con,$insert_product");
accepted to run but showing error in line 135. what mistake do i have make.full code is here
<?php require_once ("includes/db.php");
?>
<!DOCTYPE html>
<html>
<head>
<title> product</title>
</head>
<body>
<form method="post" action="insert_product.php" enctype="multipart/form-data">
<table width="700" align="center">
<tr>
<td><h2>insert new product</h2></td>
</tr>
<tr>
<td>Product title</td>
<td><input type="text" name="product_title"></td>
</tr>
<tr>
<td>Product category</td>
<td><select name="product_cat">
<option>Select a category</option>
<?php
$get_cats = "select * from category";
$run_cats= mysqli_query($con,$get_cats);
while ($row_cats=mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title=$row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Product brand</td>
<td><select name="product_brand">
<option>Select brand</option>
<?php
$get_brand = "select * from brand";
$run_brand= mysqli_query($con,$get_cats);
while ($row_brand=mysqli_fetch_array($run_brand))
{
$brand_id = $row_brand['brand_id'];
$brand_title=$row_brand['brand_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>product image1</td>
<td><input type="file" name="product_img1"></td>
</tr>
<tr>
<td>Product image2</td>
<td><input type="file" name="product_img2"></td>
</tr>
<tr>
<td>Product image3</td>
<td><input type="file" name="product_img3"></td>
</tr>
<
<tr>
<td>Product price</td>
<td><input type="number" name="product_price"></td>
</tr>
<tr>
<td>Product desceptration</td>
<td><input type="text" name="product_desc"></td>
</tr>
<tr>
<td>Product keyword</td>
<td><input type="number" name="product_keyword"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$product_title=$_POST['product_title'];
$product_cat=$_POST['product_cat'];
$product_brand=$_POST['product_brand'];
$product_price=$_POST['product_price'];
$product_desc=$_POST['product_desc'];
$status='on';
$product_keyword=$_POST['product_keyword'];
//image name
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
//image temp names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keyword=='' OR $product_img1=='' OR $product_img2=='' OR $product_img3=='')
{
echo "<script> alert ('please insert the data in the form')</script>";
exit();
}
else{
//uploadinimage to the folder
move_uploaded_file($temp_name1,"product_images/$product_img1");
move_uploaded_file($temp_name2, "product_images/$product_img2");
move_uploaded_file($temp_name3,"product_images/$product_img3");
}
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
if($row)
{
echo "<script> alert('insert sucessfully')</script>";
}
else
{
echo "<script> alert(' unsucessfull to insert')</script>";
}
}
?>
I don't think you're meaning to connect again. Rather, it appears you may be trying to perform a query?
Your code:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
Not only are you referencing a variable never defined in the script (though it probably is in the file you require at the top), but you're passing a query right to mysqli_connect(). I imagine what you actually meant to do is:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_query($con, $insert_product);

MySQL database not updating when form is submited

EDITED I am using HTML forms and PHP to upload data to a MySQL database. When I submit the information an image that i uploaded gets moved to a specific folder(the way i intended it to), but the data does not get uploaded to the data base.
I added most of the code now, the site didn't allow me to add everything, I deleted some lines that in my opinion don't have anything to do with the problem at hand. so maybe you'll see the problem:
<!DOCTYPE>
<?php
include("includes/db.php");
?>
<html>
<head>
<title>Inserting Product</title>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
</head>
<body bgcolor="grey">
<form action="insert_product.php" method="post" enctype="multipart/form-
data">
<table align="center" width="795" border="2" bgcolor="lime">
</tr>
<tr>
<td align="right"><b>Product Category:</b></td>
<td>
<select name="product_cat" >
<option>Select a Category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Publisher:</b></td>
<td> <select name="product_dev">
<option>Select a Publisher</option>
<?php
$get_devs = "select * from developers";
$run_devs = mysqli_query($con, $get_devs);
while ($row_devs=mysqli_fetch_array($run_devs)){
$dev_id = $row_devs['dev_id'];
$dev_title = $row_devs['dev_title'];
echo "<option value='$dev_id'>$dev_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Image:</b></td>
<td><input type="file" name="product_image" /></td>
</tr>
<tr>
<td align="right"><b>Product Price:</b></td>
<td><input type="text" name="product_price" required/></td>
</tr>
<tr>
<td align="right"><b>Product Keywords:</b></td>
<td><input type="text" name="product_keywords" size="50"
required/></td>
</tr>
<tr align="center">
<td colspan="7"><input type="submit" name="insert_post"
value="Insert Product Now"/></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['insert_post'])){
$product_title = $_POST['product_title'];
$product_cat= $_POST['product_cat'];
$product_pub = $_POST['product_pub'];
$product_dev = $_POST['product_dev'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
$insert_product = "insert into products
(product_cat,product_pub,product_dev,
product_title,product_price,product_desc,product_image,
product_keywords) values ('$product_cat','$product_pub','$product_dev',
'$product_title','$product_price','$product_desc',
'$product_image','$product_keywords')";
$insert_pro = mysqli_query($con, $insert_product);
if($insert_pro){
echo "<script>alert('Product Has been inserted!')</script>";
echo "<script>window.open('index.php?insert_product','_self')
</script>";
}
}
?>
Your query seems fine.
Just verify your MySQLi connection.
It should be like:
$con = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DATABASE_NAME");

SQL statement to populate more than one listbox at a time

I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>

Data some is errased when update button is pressed

Please, I am having a problem when updating data in the database through a form. When ever I press the Update button to submit any changes made to a record, all the data in the mysql fields corresponding to drop list controls is errased. I do not know what is causing this problem. Here is the code:
<?php
//include database connection
include 'db_connect.php';
// get value of object id that was sent from address bar
$c_id = $_GET['c_id'];
//check any user action
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){ //if the user hit the submit button
//write our update query
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "UPDATE collections
SET
ctitle = '".$mysqli->real_escape_string($_POST['ctitle'])."',
csubject = '".$mysqli->real_escape_string($_POST['csubject'])."',
creference = '".$mysqli->real_escape_string($_POST['creference'])."',
cyear = '".$mysqli->real_escape_string($_POST['cyear'])."',
cobjecttype = '".$mysqli->real_escape_string($_POST['cobjecttype'])."',
cmaterial = '".$mysqli->real_escape_string($_POST['cmaterial'])."',
ctechnic = '".$mysqli->real_escape_string($_POST['ctechnic'])."',
cwidth = '".$mysqli->real_escape_string($_POST['cwidth'])."',
cheight = '".$mysqli->real_escape_string($_POST['cheight'])."',
cperiod = '".$mysqli->real_escape_string($_POST['cperiod'])."',
cmarkings = '".$mysqli->real_escape_string($_POST['cmarkings'])."',
cdescription = '".$mysqli->real_escape_string($_POST['cdescription'])."',
csource = '".$mysqli->real_escape_string($_POST['csource'])."',
cartist = '".$mysqli->real_escape_string($_POST['cartist'])."'
where c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'";
//execute the query
if( $mysqli->query($query) ) {
//if updating the record was successful
echo "The record was updated.";
}else{
//if unable to update new record
echo "Database Error: Unable to update record.";
}
}
//select the specific database record to update
$query = "SELECT c_id, ctitle, csubject, creference, cyear, cobjecttype, cmaterial, ctechnic, cwidth, cheight, cperiod, cmarkings, cdescription, csource, cartist, cfilename
FROM collections
WHERE c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'
limit 0,1";
//execute the query
$result = $mysqli->query( $query );
//get the result
$row = $result->fetch_assoc();
//assign the result to certain variable so our html form will be filled up with values
$c_id = $row['c_id'];
$ctitle = $row['ctitle'];
$csubject = $row['csubject'];
$creference = $row['creference'];
$cyear = $row['cyear'];
$cobjecttype = $row['cobjecttype'];
$cmaterial = $row['cmaterial'];
$ctechnic = $row['ctechnic'];
$cwidth = $row['cwidth'];
$cheight = $row['cheight'];
$cperiod = $row['cperiod'];
$cmarkings = $row['cmarkings'];
$cdescription = $row['cdescription'];
$csource = $row['csource'];
$cartist = $row['cartist'];
$cfilename = $row['cfilename'];
?>
<!--we have our html form here where new object information will be entered-->
<table align=left>
<tr>
<td> <?php echo '<img src="./images/'.$cfilename.'" width="300" height="400" />'; ?> </td>
</tr>
<table>
<form action='#' method='post' border='0'>
<table>
<tr>
<td>TITLE</td>
<td><input type='text' name='ctitle' value='<?php echo $ctitle; ?>' /></td>
</tr>
<tr>
<td>SUBJECT</td>
<td><input type='text' name='csubject' value='<?php echo $csubject; ?>' /></td>
</tr>
<tr>
<td>REFERENCE No.</td>
<td><input type='text' name='creference' value='<?php echo $creference; ?>' /></td>
</tr>
<tr>
<td>YEAR</td>
<td><input type='text' name='cyear' value='<?php echo $cyear; ?>' /></td>
<tr><td>OBJECT TYPE</td>
<td>
<select name="cobjecttype" id="cobjecttype" tabindex="">
<option value="">---Select object type---</option>
<option value="ceramic">Ceramic</option>
<option value="clock">Clock</option>
<option value="gold">Gold and silverware</option>
<option value="mask">Mask</option>
<option value="painting">Painting</option>
<option value="sculpture">Sculpture</option>
<option value="tapestry">Tapestry</option>
</select>
</td></tr>
<tr><td>MATERIAL USED</td>
<td>
<select name="cmaterial" id="cmaterial" tabindex="" >
<option value="">---Select Material---</option>
<option value="brass">Brass</option>
<option value="oil">Oil</option>
<option value="wood">Wood</option>
<option value="carved">Canvas/Cotton/Fabric/Linen/Wool</option>
</select>
</td></tr>
<tr><td>TECHNIC</td>
<td>
<select name="ctechnic" id="ctechnic" tabindex="7" >
<option value="">---Select Technic---</option>
<option value="cast">Cast</option>
<option value="carved">Carved</option>
<option value="etched">Etched</option>
</select>
</td></tr>
<tr>
<td>WIDTH</td>
<td width="100"><input name="cwidth" type="text" id="cwidth" value="<?php echo $cwidth; ?>" size="10"></td>
</tr>
<tr>
<td>HEIGHT</td>
<td width="100"><input name="cheight" type="text" id="cheight" value="<?php echo $cheight; ?>" size="10"></td>
</tr>
<tr>
<td>PERIOD</td>
<td width="100"><input name="cperiod" type="text" id="cperiod" value="<?php echo $cperiod; ?>" size="30"></td>
</tr>
<tr>
<td>MARKINGS</td>
<td width="100"><input name="cmarkings" type="text" id="cmarkings" value="<?php echo $cmarkings; ?>" size="30"></td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td width="400"><textarea name="cdescription" rows="2" cols="50" id="cdescription" value="<?php echo $cdescription; ?>"></textarea></td></tr>
<tr>
<td>SOURCE</td>
<td width="100"><input name="csource" type="text" id="csource" value="<?php echo $csource; ?>" size="30"></td>
</tr>
<tr>
<td>ARTIST</td>
<td width="100"><input name="cartist" type="text" id="cartist" value="<?php echo $cartist; ?>" size="30"></td>
</tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='c_id' value='<?php echo $c_id ?>' />
<!-- we will set the action to update -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Save' />
<a href='gallery.php'>Back to display page</a>
</td>
</tr>
</table>
</form>
Can someone help to identify what the problem is?
Such problem occur when you dont validate your POST data correctly. In your code, you are updating your records directly, by using mysql_real_escape_string($variable). But although this might fix some security issues will not validated every data if it is present or not.
Validate your variables to be present and hold data before updating to the query.
you post a form with the method POST, but get the c_id with $_GET
change it to $_POST['c_id'] or $_REQUEST['c_id'] ...

Form Select List to show Product Names from Mysql Database

I dont get what im doing wrong here..
i have about 125 product in the table but i get only the last product from the table so it shows only one item... this is a simple calculator to provide the sales person and the customer how much box they would need and how much it would cost a quick estimate.
Thank you for the help in advance..
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
$TileNameList = "<option value=\"$sqft\">$isim $boyut</option>";
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>
Your loop doesn't append to $TileNameList because it exists outside of it. It actually replaces it's value. Try:
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
$TileNameList .= "<option value=\"$sqft\">$isim $boyut</option>"; // NOTE THE .=
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>

Categories