Im trying to add create a product page and I have all the relevant column names in the product table but the loop won't work for some reason :(
<?php
$dbhost = 'localhost';
$dbuser = 'CU4507408';
$dbpass = '*********';
$dbname = 'CU4507408';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname);
$query = "SELECT * FROM product WHERE id=".$_REQUEST['productID'].";";
$result = mysql_query($query) or die("failed!");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$productID = $row['productID'];
$name = $row['name'];
$price = $row['price'];
$manufacturer = $row['manufacturer'];
$rating = $row['rating'];
$categoryID = $row['categoryID'];
$productinfo = $row['productinfo'];
$image = $row['image'];
$youtube = $row['youtube'];
}
?>
There error I get is
Undefined index: productID in /home/4507408/public_html/viewproduct.php on line 8 failed!
productID is the primary key of the product table
Thanks :)
$_REQUEST['productID'] is undefined.
Your URL (if you're using GET), should look like this:
www.example.com/products?productID=3
If the productID part is missing, it becomes undefined.
Related
I want to get some data from database with this code
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $bd);
$result = mysql_query("SELECT author_id FROM user");
while($data = mysql_fetch_array($result)) {
$sn=$data['author_id'];
}
$lastresult = mysql_query("SELECT * FROM post WHERE id='".$sn."'");
This code only work fetches one author_id
$sn=$data['author_id'] and $lastresult works only with one author_id and doesn't fetches all the author_id.
How can I get data for all author_id ?
You are overwritting $sn each loop.
$result = mysql_query("SELECT author_id FROM user");
$sn = [];
while($data = mysql_fetch_array($result)) {
$sn[] = $data['author_id'];
}
$sn = implode(',',$sn);
$lastresult = mysql_query("SELECT * FROM post WHERE id IN (".$sn.")");
PS: mysql_* functions are deprecated and has security issues. Consider replace with mysqli_* functions or PDO.
im currently trying to extract a table from my database (articles) and the table article and put it in an array but im not sure weather or not it wokred because i dont know how to print an array. i was following this link.
http://phpscriptarray.com/php-arrays-tutorials-tour/how-to-extract-mysql-database-data-into-php-array-variable.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// create connection
$conn = new mysqli($servername, $username, $password);
// check connection
if ($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
// connect to DB
$db_selected = mysqli_select_db('article', $conn);
if (!$db_selected){
die("can't use article : " .mysqli_error());
}
// extract databases table to PHP array
$query = "SELECT * FROM `articles`";
$result = mysqli_query($query);
$number = mysql_numrows($result);
$article_array = array();
$x = 0
while($x < $number)
{
$row = mysqli_fetch_array($result);
$artic = $row['name'];
$amount = $row['quantity'];
$article_array[$artic] = $amount;
$x++;
}
echo count($article_array);
//echo "hello";
<?
even the echo hello wont work and im not sure if i was supposed to put a name and quantity in:
$artic = $row['name'];
$amount = $row['quantity'];
You are mixing object oriented with procedural style. Your query and loop should look like this:
$query = "SELECT * FROM `articles`";
$result = $conn->query($query);
$article_array = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$artic = $row['name'];
$amount = $row['quantity'];
$article_array[$artic] = $amount;
}
http://php.net/manual/en/mysqli.query.php
Also your PHP closing tag is faulty - should be ?> or omitted.
I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");
I have my grid running and I can EDIT data but I cannot ADD new rows of data. When I submit the add form nothing happens and I get nothing back on my PHPMyAdmin page to show that the grid is hit the database with the INSERT statement.
Here is my Edit code.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*****";
$dbname = "******";
// connect to the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());
mysqli_select_db($conn,$dbname) or die("Error conecting to db.");
//$id = "'".$_POST['id']."'";
$unit = "'".$_POST['unit_id']."'";
$div = "'".$_POST['div_id']."'";
$titleorg = "'".$_POST['title_org']."'";
$shortdesc = "'".$_POST['short_desc']."'";
$longdesc = "'".$_POST['long_desc']."'";
$unitdesc = "'".$_POST['unit_desc']."'";
$enabled = "'".$_POST['avail_ind']."'";
$active = "'Y'";
switch($_POST['oper'])
{
case "add":
$query = "INSERT INTO depunits (unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind,active_ind) values ($div,$unit,$titleorg,$shortdesc,$longdesc,$unitdesc,$enabled,$active)";
mysqli_query($conn,$query);
break;
case "edit":
$query = "UPDATE depunits SET unit_id=$unit,div_id=$div,title_org=$titleorg,short_desc=$shortdesc,long_desc=$longdesc,unit_desc=$unitdesc,avail_ind=$enabled where unit_id=$unit";
mysqli_query($conn,$query);
break;
}
?>
This is the code which I have used; the problem is only 1 item's
quantity can be decremented at a time; multiple item quantities cannot
be decremented.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "yumyum";
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb) or die("database selection error");
$id = $_POST['id'];
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
$rows = array();
while($r = mysql_fetch_assoc($query1)) {
$output = $r['Quantity'];
echo $output;
$query2=mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` LIKE ".$r["id"]);
}
?>
Can you try this,
$query2 = mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` = '".$r["id"]."' ");
OR
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` = '".$r["id"]."' ");
$rows = array();
while($r = mysql_fetch_assoc($query1)) {
$output = $r['Quantity'];
$output = $output-1;
$query2=mysql_query("UPDATE food SET Quantity = '".$output."' where `food`.`id` = '".$r["id"]."' ");
}
Since its sending one id at a time after $id = $_POST['id'];
you only need to do this:
mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` = $id");
and remove all other lines underneath it.
chech no# of records after record query. For example for your code,
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "yumyum";
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb) or die("database selection error");
$id = $_POST['id'];
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
echo mysql_num_rows($query1); // this will show you count of records by above select query make sure you have more than one item count to update multiple record.
$rows = array();
while($r = mysql_fetch_assoc($query1)) {
$output = $r['Quantity'];
echo $output;
$query2=mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` LIKE ".$r["id"]);
}
?>
if you are geeting 1 record in select query then use wild cards in your query like,
$query1 = mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE '%".$id."%'");
try to use wildcard according to your DB structure.