This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I have a error problem with my code in php. can anyone help me to correct it!
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
echo '</tr>';
};?>
In Chrome i get this notif:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\delivery\pengiriman.php on line 33.
I don't know what is wrong because i am new with php.
And btw line 33 is
<form method="post" action="pengiriman-input.php">
there is some syntex error while putting HTML between PHP near your form tag
<?php $query_tbl = "SELECT `transaksi.id_transaksi`, `pelanggan.nama` FROM `transaksi`, `pelanggan` WHERE
`transaksi.id_pelanggan` = `pelanggan.id_pelanggan` ORDER BY `id_transaksi`";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';?>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php echo '</tr>';
};?>
Try this more simple and neat code
<?php
$query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ". "WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl) or die('Error select table');
?>
<?php while($row = mysqli_fetch_array($list_tbl)): ?>
<tr>
<td><?php echo $row['id_transaksi']; ?></td>
<td><?php echo $row['nama']; ?></td>
<td>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
</td>
</tr>
<?php endwhile; ?>
You can't put HTML directly into PHP
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
?> <!-- here ends the PHP -->
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php // here begin PHP again
echo '</tr>';
};?>
You need to end the php then start it again. Try this:
<?php $query_tbl = "SELECT transaksi.id_transaksi, pelanggan.nama FROM transaksi, pelanggan ".
"WHERE transaksi.id_pelanggan = pelanggan.id_pelanggan ORDER BY id_transaksi";
$list_tbl = mysqli_query($dbc, $query_tbl)
or die('Error select table');
while($row = mysqli_fetch_array($list_tbl))
{
echo '<tr>';
echo '<td>' . $row['id_transaksi'] . '</td>';
echo '<td>' . $row['nama'] . '</td>';
?>
<form method="post" action="pengiriman-input.php">
<input type="hidden" name="id" value="<?php echo $row['id_transaksi']; ?>" />
<input type="submit" value="Kirim" />
</form>
<?php
echo '</tr>';
};?>
In the above snippet you are opening a php tag in line 1 and have thought of closing it at last line. Between these two tag you cannot write the html script for creating form, as you are doing, in a php file.
It will be taken as a php script instead of html script and php compiler don't understands html tags.
Create the form tag as a php string variable and echo it as you are doing in the lines above.
Related
I use a Wordpress site, I do my search on a custom database called "operations". The search is performed on the website.
I need to get other results from the table related to this row on request, not just what I entered. And get other data related to this string. Here is the search form on the site:
<form method="post" action="https://site-name.com/wp-content/themes/theme/select_user.php">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<input id="submit" type="submit" value="Search"><br/>
</form>
</fieldset>
The database has the following columns: id, date, title, size, sku, barcode, price
File Contents select_user.php:
require( __DIR__ . '/../../../wp-load.php' );
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT * FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode',
ARRAY_N
"
)
);
if ($sql_select)
{
foreach($sql_select as $row)
{
echo 'SKU: ' . $row['sku'] .'</br>';
echo 'Barcode: ' . $row['barcode'] .'</br>';
}
}
else {
echo 'No results';
}
With this code, I get the answer "no results".
I would be grateful for any help
use the post hook for wp forms not directly use .php file
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
// Handle request then generate response using echo or leaving PHP and using HTML
}
for your form use :
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="add_foobar">
<input type="hidden" name="data" value="foobarid">
<input type="submit" value="Submit">
</form>
I created my own shortcode, and placed everything that is necessary there. Everything works.
function custom_search_func( $atts ){
echo '<fieldset>
<form action="' . get_permalink() . '" method="POST">
<label for="sku">SKU:</label><br/>
<input type="text" name="sku" size="30"><br/>
<label for="barcode">Barcode:</label><br/>
<input type="text" name="barcode" size="30"><br/>
<label for="date">Date:</label><br/>
<input type="date" name="date" size="30"><br/>
<input type="submit" value="Search">
</form>
</fieldset>';
global $wpdb;
$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);
$date = trim($_REQUEST['date']);
$sql_select = $wpdb->get_results(
$wpdb->prepare(
"
SELECT id, date, title, size, barcode, sku, price FROM " . $wpdb->prefix . "operations
WHERE sku='$sku' || barcode='$barcode' || date='$date'
"
)
);
if ($sql_select)
{
echo '<table border="1" width="100%" cellpadding="5">';
echo '<tr><th>ID</th>';
echo '<th>Title</th>';
echo '<th>Size</th>';
echo '<th>SKU</th>';
echo '<th>Barcode</th>';
echo '<th>Price</th>';
echo '<th>Date</th></tr>';
foreach ( $sql_select as $id) {
echo '<tr>';
echo '<td>';
echo $id->id;
echo '</td>';
echo '<td>';
echo $id->title;
echo '</td>';
echo '<td>';
echo $id->size;
echo '</td>';
echo '<td>';
echo $id->sku;
echo '</td>';
echo '<td>';
echo $id->barcode;
echo '</td>';
echo '<td>';
echo $id->price;
echo '</td>';
echo '<td>';
echo $id->date;
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo 'No results';
}
}
add_shortcode( 'customsearch', 'custom_search_func' );
Use the [customsearch] shortcode to output this anywhere on the page
here is a bit of code im working on. Everything works expect that part where i enter ID, then it shows me information in that ID row and also 2 boxes pop where i can write new value of variable. There is also 2 buttons, one which comfirms the edit which has been written into boxes and second one to delete that row from database.
Im still at kinda basics at programming as you can see and im quessing there is something wrong with structure in this part of code?
Can anyone help me to make these 2 submit buttons work as they should.
Thanks you all. :)
if(isset($_POST['idnumber'])) {
$idnumber = protect($_POST['idnumber']);
global $idnumber;
if($idnumber == ""){
echo "Please supply all fields!";
}
else{
?>
<table cellpadding="2" cellspacing="2">
<br />
<tr>
<td width="50px"><b>ID</b></td>
<td width="150px"><b>Current name</b></td>
<td width="200px"><b>Current description</b></td>
<td width="100px"><b>Time added</b></td>
<td width="100px"><b>Current status</b></td>
</tr>
<?php
$get_tasks = mysqli_query($con, "SELECT id FROM task WHERE id='$idnumber'") or die(mysql_error());
while($row = mysqli_fetch_assoc($get_tasks)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
$get_taskname = mysqli_query($con, "SELECT taskname FROM task WHERE id='$idnumber'") or die(mysql_error());
$task_name = mysqli_fetch_assoc($get_taskname);
echo "<td>" . $task_name['taskname'] . "</td>";
$get_description = mysqli_query($con, "SELECT description FROM task WHERE id='$idnumber'") or die(mysql_error());
$description_text = mysqli_fetch_assoc($get_description);
echo "<td>" . $description_text['description'] . "</td>";
$get_dateadded = mysqli_query($con, "SELECT dateadded FROM task WHERE id='$idnumber'") or die(mysql_error());
$date_added = mysqli_fetch_assoc($get_dateadded);
echo "<td>" . $date_added['dateadded'] . "</td>";
$get_status = mysqli_query($con, "SELECT status FROM task WHERE id='$idnumber'") or die(mysql_error());
$cur_status = mysqli_fetch_assoc($get_status);
if($cur_status['status'] == 1) {
echo "<td>" . "Unfinished" . "</td>";
}
else{
echo "<td>" . "Finished" . "</td>";
}
echo "</tr>";
}
if(isset($_POST['edittask'])) {
$editname = protect($_POST['editname']);
mysqli_query($con, "UPDATE task SET taskname=$editname WHERE id='$idnumber'") or die(mysql_error());
}
elseif(isset($_POST['deletetask'])) {
mysqli_query($con, "DELETE FROM task WHERE id='$idnumber'") or die(mysql_error());
}
?>
<form action="test2.php" method="POST" >
<td width="50px"><b></b></td>
<td width="150px"><b>New name: <input type="text" name="editname"/></b></td>
<td width="200px"><b>New desc: <input type="text" name="editdesc"/></b></td>
<td width="100px"><b></b></td>
<td width="100px"><b>D</b></td>
<input type="submit" name="edittask" value="Edit task"/> <br />
<input type="submit" name="deletetask" value="Delete task"/> <br />
</form>
<?php
}
}
?>
</table>
<br /><br />
<form action="test2.php" method="POST" >
Insert ID of task to edit: <input type="text" name="idnumber"/>
<input type="submit" name="ids" value="Find task"/> <br />
</form>
This is a table of students' ID numbers, with a button after each ID.
When I click on a button, I want it to open a new page called "score.php", and display the selected ID.
But the code doesn't work. It only show the text "ID", but not the number.
Here is "index.php"
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $_POST['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
And here is "score.php"
<head>test</head>
<body>
<?php
$student_id = $_POST["student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
</body>
Since you are using a <button>, there is no need to use a <input type="hidden">. Just add the student_id as the button value -
<button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button>
Then in you php just get the value from the clicked on button -
<?php
$student_id = $_POST["btn_student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
your index.php file will be:
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $row['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
I have a database containing books. On a page, I have loop that prints each record in the database which each book's title, author, publisher, date, and rating. I want to use a delete button at the bottom of each record in order to delete it.
When I click on the delete button, the page is updated, but the record is not deleted.
I have tried to find solutions to this problem, but have yet to. I tried to use the book_id category in my database table as my index, in order to identify each record and delete it but it does not work.
Here is the code for my button form, as it appears with html code:
while ($row = mysqli_fetch_array($result))
{
echo '<div id="forminput">';
$timestamp = strtotime($row['date']);
echo '<div id = "bookpicture">';
echo '<img src="images/Book Cover 8crop.jpg" alt="Book Cover">';
echo '</div>';
echo '<div id = "bookinfo">';
echo '<div id = "titleauthor">';
echo '<h3>' . htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') . '</h3>';
echo '<p>' . htmlspecialchars($row['author'], ENT_QUOTES, 'UTF-8') . '</p>';
echo '</div>';
echo '</div>';
$id = htmlspecialchars($row['book_id'], ENT_QUOTES, 'UTF-8');
echo '</div>' ;
echo '<div id = "publisher">' ;
echo '<p>' . 'Published on' . " " . htmlspecialchars(date('F j, Y,', $timestamp),
ENT_QUOTES, 'UTF-8') .
" " . 'by' . " " . htmlspecialchars($row['publisher'], ENT_QUOTES, 'UTF-8') . '</p>';
echo '</div>';
echo '<div id = "formDelete">';
echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">';
echo '<input type="submit" value="Update" name="myAdd" id="myAdd" style = "width:
100px" required>';
echo '<input type="submit" value="Delete" name="myDelete" id="$id" style = "width:
100px" required>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<hr>' ;
echo '</div>';
}
?>
Here is the code from my index.php file.
else if (isset($_POST['myDelete']))
{
$ids = mysqli_real_escape_string($link, $_POST['$id']);
$sql="DELETE FROM readbooks WHERE book_id = '$ids'";
if (!mysqli_query($link, $sql))
{
$error = 'Error with submission: ' . mysqli_error($link);
include 'php/error.html.php';
exit();
}
}
Here is the updated code.
The Problem is you are not trying to send the row ID from the form.
In Form try sending row id from the form
echo '<input type="hidden" name="id" value="$id">'
try receiving that parameter
$id = $_POST['id'];
$con = mysql_connect("host address","mysql_user","mysql_pwd");
$query = "DELETE FROM readbooks WHERE id = $id";
mysql_query($query,$con);
Moving from comment, you're not actually getting the $id anywhere. Add a field to your form:
<input type='hidden' name='id' value='$id'>
and then refer to it in your php:
$ids = mysqli_real_escape_string($link, $_POST['id']);
Since your using mysqli now, use prepared statements. Do not directly use your user input to the query! Example:
$books = array();
// connection
$con = new mysqli('localhost', 'your_username', 'password_of_username', 'your_database');
if(isset($_POST['myDelete'])) {
$book_id = $_POST['myDelete']; // get the variable id
$stmt = $con->prepare('DELETE FROM readbooks WHERE book_id = ?');
$stmt->bind_param('i', $book_id);
$stmt->execute();
}
$result = mysqli_query($con, 'SELECT * FROM readbooks');
while($row = $result->fetch_assoc()) {
$books[] = $row;
}
?>
<form method="POST">
<table border="1" cellpadding="10">
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th></th>
</tr>
<?php foreach($books as $book): ?>
<tr>
<td><?php echo $book['title']; ?></td>
<td><?php echo $book['author']; ?></td>
<td><?php echo $book['publisher']; ?></td>
<td><button type="submit" name="myDelete" value="<?php echo $book['book_id']; ?>">Delete</button></td>
</tr>
<?php endforeach; ?>
</table>
</form>
I think you're having problem with the id that you are passing to your PHP code. Try to use var_dump($_POST) to see what is the content of your $_POST variable. As I can see, the index of $_POST is wrong.
<input type="submit" value="Delete" name="myDelete" id="$id" style = "width:100px" required>';
Try to change this to
<input type="submit" name="book_id" value="$id" style = "width:100px" required>';
In your php use this $_POST["book_id"] to get the value of book_id you posted.
Note : The attribute "name" of the input tags will be the index of your $_POST variable
I understand that you want to have a delete and update button at the same time. The problem is your logic behind the situation. You need to have a hidden field and put the id inside it. Try to use this.
echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">';
echo '<input type="hidden" value="$id" name="book_id" id="myAdd" required>';
echo '<input type="submit" value="Update" name="action" id="myAdd" required>';
echo '<input type="submit" value="Delete" name="action" id="$id" required>';
echo '</form>';
In your php code use try to have a condition like this :
$book_id = $_POST["book_id"];
if($_POST["action"] == "delete")
{
//do your deletion code here. use the variable $book_id
}
else{
//do your update code here.use the variable $book_id
}
I'm trying to create insert, update and delete relate to shopping, all of them are working fine except update page.
Please see sample code of list of product from index.php page ...
<?php
$sql = "SELECT product_id, product_name, product_category, product_retail_price, product_price, product_detail FROM product";
$result_db = $db->query($sql) or die('Error perform query!');
?>
<table border="1">
<tr>
<th>product name</th>
<th>product category</th>
<th>product retail price</th>
<th>price</th>
<th>detail</th>
<th>Update</th>
<th>Delete</th>
<th>Insert</th>
</tr>
<?php
while ($r = $result_db->fetch_object()) {
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
$delete = "delete.php?product_id={$r->product_id}";
$insert = "insert.php";
echo '<tr>';
echo '<td>' . $r->product_id . '</td>';
echo '<td>' . $r->product_name . '</td>';
echo '<td>' . $r->product_category . '</td>';
echo '<td>' . $r->product_retail_price . '</td>';
echo '<td>' . $r->product_price . '</td>';
echo '<td>' . $r->product_detail . '</td>';
echo "<td><a href='{$update}'>Update</a></td>";
echo "<td><a href='{$delete}'>Delete</a></td>";
echo "<td><a href='{$insert}'>Insert</a></td>";
echo '</tr>';
}
$db->close();
?>
</table>
</body>
As you can see above code where it said
while ($r = $result_db->fetch_object()) {
$update = ......
This sending the data of relate to product using "product_id" sending sending to the updateform.php page ... that updateform.php page is the code showing
<body>
<form action="update.php" method="post">
<input type="hidden" value="<?= $_GET['product_id'] ?>" name="product_id"/>
product name: <input type="text" name="product_name" value="<?= $_GET['product_name'] ?>">
product category: <input type="text" name="product_category" value="<?= $_GET['product_category'] ?>">
product retail price: <input type="text" name="product_retail_price" value="<?= $_GET['product_retail_price'] ?>">
product price: <input type="text" name="product_price" value="<?= $_GET['product_price'] ?>">
product detail: <input type="text" name="product_detail" value="<?= $_GET['product_detail'] ?>">
<input type="submit" name="submit">
</form>
</body>
</html>
When I run the code, the updateform.php showing the text field with <?= $_GET['xxxxxx'] ?>"
Why am I getting this result?
Is it this code?
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
Is it the right code to get right information from database using "product_id" number? Is there better code to write than this?
Try using :
<?php echo $_GET['xxxxxx']; ?>
and if it works, it means that in your php configuration the use of short opening tags is restricted. meaning no