shopping cart with PHP and connected to the MySQL - php

Good evening, I am currently working on the implementation of a shopping cart through PHP and MySQL, I am getting the following error at line 172 onwards, I have been looking at the quotes but I cannot find the problem, what could be the solution to this?
ERROR : Parse error: syntax error, unexpected 'img' (T_STRING), expecting ';' or ',' in B:\XAMPP\htdocs\dashboard\PHP_Assessments\Sport_Cars\cart.php on line 171
<div class="container" style="width:700px;">
<h3 align="center">Simple PHP Mysql Shopping Cart</h3><br />
<?php
$sql = "SELECT * FROM sport_cars.cars ORDER BY carID ASC";
//prepared statement
$statement = $conn->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
foreach($result as $row):
echo "<div class='col-md-4'>";
echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ' ">";
echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . " /><br/> ";
echo "<h4 class='text-info'> . $row['carName'] . </h4> ";
echo "<h4 class='text-danger'>.$row['carPrice']; . "</h4> ";
echo "<input type='text' name='quantity' class='form-control' value='1' /> ";
echo "<input type='hidden' name='hidden_name' value=.' ['carPrice']; '. /> ";
echo "<input type='hidden' name='hidden_price' value=.' $row['carPrice']; '. /> ";
echo "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn btn-success' value='Add to Cart' /> ";
echo "</div></form></div>";
endforeach;
if($result > 0)
{
while($row = $statement -> fetchAll())
{
?>
<div class="col-md-4">
<form method="post" action="cart.php?action=add&id=<?php echo $row["carID"]; ?>">
<div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
<img src="<?php echo $row["Photo"]; ?>" class="img-responsive" /><br />
<h4 class="text-info"><?php echo $row["carName"]; ?></h4>
<h4 class="text-danger">$ <?php echo $row["carPrice"]; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="hidden" name="hidden_name" value="<?php echo $row["carPrice"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["carPrice"]; ?>" />
<input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />
</div>
</form>
</div>
<?php
}
}
?>
<div style="clear:both"></div>
<br />
<h3>Order Details</h3>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="40%">Item Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Total</th>
<th width="5%">Action</th>
</tr>
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?php echo $values["carName"]; ?></td>
<td><?php echo $values["quantity"]; ?></td>
<td>$ <?php echo $values["carPrice"]; ?></td>
<td>$ <?php echo number_format($values["quantity"] * $values["carPrice"], 2); ?></td>
<td><span class="text-danger">Remove</span></td>
</tr>
<?php
$total = $total + ($values["quantity"] * $values["carPrice"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
I have been changing this cart from a mysqli version of it into pdo and I am having some trouble to make it work, I hope this can be fixed.
Thanks for your help.

You had some errors in your foreach loop containing $result so try this:
foreach($result as $row){
echo "<div class='col-md-4'>";
echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ">";
echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . "
/>
<br/> ";
echo "<h4 class='text-info'>" . $row['carName'] . "</h4> ";
echo "<h4 class='text-danger'>".$row['carPrice'] . "</h4> ";
echo "<input type='text' name='quantity' class='form-control' value='1' /> ";
echo "<input type='hidden' name='hidden_name' value='" . $row['carPrice'] . "'/>
";
echo "<input type='hidden' name='hidden_price' value='" . $row['carPrice'] .
"'/> ";
echo "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn
btn-
success' value='Add to Cart' /> ";
echo "</div></form></div>";
}
You could try using printf to help with readability of the code. http://php.net/printf

It's PHP syntax error. You ca refer to the screenshot below for some error as example. You need to make sure there is no error first before getting it works

NO NO NO!
When you use
echo "<div></div>"; You never Put a " Inside of two "" You can only use ''
echo "<div class=""></div>";Wrong
echo "<div class=''></div>";Correct
-- LETS LOOK AT THIS LINE --
echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ' ">";
You are doing this wrong! First of all This is in Double Quotes So you don't need the . $row['carId'] .
The fixed code would be :
$id = $row['id'];
echo "<form method=\"Post\" action=\"car.php?action=add&id=$id\" >";
You would need to Change your whole code :(

Related

php table array direction

<div class="section" id="section2">
<div class="slide" id="slide1">
<div class="box">
<div class="haha1">Library</div>
<p>
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
<br>
<center>Page 1</center>
</div></div>
<?php
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result))
{
?>
<table class="box1" width="700" border="1">
<tr>
<td><img src="<?php echo $row['image']; ?>" height="250" width="150"><br>
<?php echo $row['bookname']; ?><br>
<?php echo $row['author']; ?><br>
<input type="submit" name="add" value="Add Cart"></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<?php } ?>
This is my table when I add new item list down vertically but I want array item horizontally example [item1,item2,item3,item4] after array 4 item horizontally only create another td then array again horizontally 4 time.
Required output:
{1,2,3,4}
{5,6,7,8}
array from left to right
else array vertically {1,5} {2,6},{3,7],[4,8]
try change the table this way
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
for testing the echo try a simple case like this (only the button)
echo "<td><input type='submit' name='add'
value='Add Cart'></td>";
In this sample (I have not you db an your IMG so i have change the var value with string value you can see 2 row of pic 4 in the first and 2 in the second.
Then if you don'obtain the same result could be you have some problem with your select or your value..
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =4 ;
$book = 0;
/* $sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){*/
while( $book<=5 ){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
/* echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";*/
echo "<td><img src='" . "row['image']" .
"' height='250' width='150'><br />" ."row['bookname']<br />" . "row['author']<br />" . "<input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
$book++;
}
?>
</table>

Not deleting values from Database using checkboxes

I have a quick question, I'm trying to use a function to delete a certain movie from a database using checkboxes. I can add stuff to the database just fine, but can't seem to delete using the checkboxes. Thanks in advance for your help!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<?
$db = new PDO("mysql:host=localhost;dbname=armstrongpz", "armstrongpz", "12345");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<?
function deleteFromDatabase($db)
{
$deleteQuery = $db->prepare("SELECT * FROM movie");
$deleteQuery->execute();
foreach($deleteQuery->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$deleteQuery = $db->prepare('DELETE FROM movie WHERE movie.id='. $row['id']);
$deleteQuery->excute();
}
}
}
?>
<h2 style='text-align: center';>
Movie Collection Database
</h2>
<?
if($_SERVER['REQUEST_METHOD'] === "POST")
{
deleteFromDatabase($db);
if(isset($_POST['add']))
{
$TitleOfMovie = $_POST['TitleMovie'];
$StudioOfMovie = $_POST['StudioMovie'];
$RatingOfMovie = $_POST['mRating'];
$PubYearOfMovie = $_POST['PubYear'];
$IMDBRating = floatval($_POST['imdbRating']);
$RunTimeOfMovie = $_POST['RunTime'];
$addQuery = "INSERT INTO movie (id,title,studio,rating,pub_year,imdb_rating,run_time) VALUES(".'NULL'.", '$TitleOfMovie','$StudioOfMovie','$RatingOfMovie','$PubYearOfMovie', '$IMDBRating', '$RunTimeOfMovie')";
$statement = $db->prepare($addQuery);
$statement->execute();
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<table align='center' border='0' cellpadding='5'>
<tbody>
<tr>
<td style='border-style: none'>
<input name='TitleMovie' size='50' type='text' width='35' value= 'Title'/>
</td>
<td style='border-style: none'>
<input name='StudioMovie' size='25' type='text' width='35' value= 'Studio Name'/>
</td>
<td>
<?
$rating = array(1=>'G', 'PG', 'PG-13', 'R', 'NC-17', 'Not Rated');
echo "<select size='1' selected='movieRating' name='mRating'>";
foreach($rating as $key=>$value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>\n";
?>
</td>
<td style='border-style: none'>
<input name='PubYear' size='10' type='text' width='35' value='Pub Year'/>
</td>
<td style='border-style: none'>
<input name='imdbRating' size='10' type='text' width='35' value='IMDB rating'/>
</td>
<td>
<input name='RunTime' size='10' type='text' width='35' value='Run time'/>
</td>
<td>
<input type="checkbox" name="add" value="Add" align="top"/>Add
</td>
</tr>
<div style="text-align:center">
<input type='submit' name='submit' value='Update Database'>
</div>
</tbody>
</table>
</form>
<table border ='1' align='center'>
<?
$arrayOfFieldNames = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run time(min)");
echo "<tr>";
echo "<td>". $arrayOfFieldNames[0] . "</td>";
echo "<td>". $arrayOfFieldNames[1] . "</td>";
echo "<td>". $arrayOfFieldNames[2] . "</td>";
echo "<td>". $arrayOfFieldNames[3] . "</td>";
echo "<td>". $arrayOfFieldNames[4] . "</td>";
echo "<td>". $arrayOfFieldNames[5] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
?>
<?
$query = "SELECT * FROM movie";
$stmt = $db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "<td>" .$row['studio']. "</td>";
echo "<td>" .$row['rating']. "</td>";
echo "<td>" .$row['pub_year']. "</td>";
echo "<td>" .$row['imdb_rating']. "</td>";
echo "<td>" .$row['run_time']. "</td>";
echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'>Delete</td>";
echo "</tr>";
}
?>
</table>
<?
if(isset($db))
{
$db= null;
}
?>
</body>

link not redirecting to another page

i have a table where one column is for edit button the code for it is
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
i don't know why but the link is not working. Can anyone tell me whats wrong with it
the code for entire table is
<form method="post" action="sendmail.php">
<div class="tab_container">
<div id="tab1" class="tab_content">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo "<table class='tablesorter' cellspacing='0'> ";
echo "<thead>
<tr>
<th></th>
<th></th>
<th>Shopname</th>
<th>Instagram account</th>
<th>Favourite</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
mysqli_data_seek($result, $i);
$data = mysqli_fetch_assoc($result);
echo "<tr " . $cls . ">";
echo '<td></td>';
?><td> <input name="checkbox[<?php echo $data['id']?>]" type="checkbox"> </td>
<?
echo '<td>' . $data['fullname_shopname'] . '</td>';
echo '<td>' . $data['username_instagramacnt'] . '</td>';
if($data['staffpick']=='yes')
{
echo '<td>Yes</td>';
}
else
{
echo '<td>No</td>';
}
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_vendor.php?id=".$data['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";
echo "</tr>";
}
echo "</table>";
echo '<div class="pagination" style="margin-left:300px;" >';
if ($total_pages > 1)
{
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul>
</div>";
?>
</div>
</div>
<footer>
<div class="submit_link">
<input type="submit" value="Send Mail" name="submit" class="alt_btn">
</div>
</footer>
</form>
If your <table> is wrapped in a <form> tag. Use an <img /> tag instead. Using <input type="image" /> will actually submit the form rather than to perform a link redirection.
echo "
<td>
<a href=\"a_add_fav_vendor.php?id=".$data['id']."\">
<img src=\"images/icn_edit.png\" title=\"Edit\"
</a>
</td>
";
Try this :
echo '<td><input type="image" src="images/icn_edit.png" title="Edit"></td>';
Do some Change like
echo "<td><a href='a_add_fav_vendor.php?id=".$data['id']."'><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Executing two php variables in mysql where query

my html form ->
<form action="where.php" method="post">
<h2 align="center" style="color: white;"> Search Challan </h2>
<table border="1" bgcolor="grey" align="center">
<tr>
<td align="center"> Search a Challan Details . Enter the Challan no below :</td>
</tr>
<tr>
<td align="center">
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Search"align="middle" ></td>
<tr>
<td>
<select name="squery" style="width:142px;" >
<option value="challan_no">Challan no </option>
<option value="product_name">Product Name</option>
<option value="Buyer">buyer</option>
<option value="Employee Responsible">Employee</option>
</td>
</tr>
<tr>
<td>
<input type="text" name="search" >
</td>
</tr>
</tr>
</form>
where.php ->
<?php
$dbhost='localhost';
$dbusername='root';
$dbuserpass='';
$dbname='inventory';
//connect to the mysql database server.
$con = mysql_connect ($dbhost, $dbusername, $dbuserpass);
if (!$con ) die ("unable to connect : ". mysql_error());
mysql_selectdb("$dbname",$con ) ;
$user_req = $_REQUEST['squery'] ; //colomn name
$req_id = $_REQUEST['search'] ; //
$query = "SELECT * FROM challan WHERE '$user_req' = $req_id ";
$result = mysql_query($query);
if (!$result) die ("DAtabase acces faild bc : ". mysql_error());
$rows = mysql_numrows($result);
for ($j=0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo "<TABLE border=1 bgcolor=grey align=center width=500px float=left>" ;
echo "<tr>";
echo "<td align=center>Challan no : </td>";
echo " <td> $row[0] </td>";
echo " </tr>";
echo "<tr>";
echo "<td align=center>Challan Date : </td>";
echo " <td> $row[1] </td>";
echo "<tr>";
echo "<td align=center>Product Name : </td> ";
echo " <td> $row[2] </td>";
echo "<tr>";
echo "<td align=center>Product qty : </td> " ;
echo " <td> $row[3] </td>";
echo "<tr>";
echo "<td align=center> Buyer : </td> " ;
echo " <td> $row[4] </td>";
echo "<tr>";
echo "<td align=center>Employee Responsible : </td> " ;
echo " <td> $row[5] </td>";
echo "</tr>";
}
?>
Its output is just blank. I'm unable to identify where I am going wrong.
blank screen indicates that has occurred a fatal error and php was unable to continue script execution, you can run this in the top of your scripts to errors should be printed to the screen as part of the output.
<?php
ini_set('display_errors', 1);

Categories