I've got an error in my code, I've been googling it and trying to find out what the problem is. As far as I know it's been a problem executing my sql code (around variable $so). Could anyone help me out?
Fatal error: Call to a member function execute() on a non-object in ... on line 15
<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
$con = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$so = $con->prepare("SELECT * FROM besteloverzicht");
$so->execute();
$result = $so->get_result();
echo "<form name='overzicht' method='post'>";
echo "<table align='center' border='2'>
<tr>
<th>Ordernr</th>
<th>Klantnaam</th>
<th>Productnaam</th>
<th>ProductID</th>
<th>Status</th>
<th>Verwijderen</th>
</tr>";
while($row = $result->fetch_assoc()) {
$ordernr = $row['ordernr'];
$klantnaam = $row['klantnaam'];
$productnaam = $row['productnaam'];
$productid = $row['productid'];
$status = $row['status'];
echo "<tr>";
echo "<td width='150px'>" . $ordernr . "</td>";
echo "<td width='150px'>" . $klantnaam . "</td>";
echo "<td width='300px'>" . $productnaam . "</td>";
echo "<td width='100px'>" . $productid . "</td>";
echo "<td width='200px'><select name='status[$ordernr]'>
<option>" . $status . "</option>";
if($row['status'] != "Niet besteld")
echo "<option>Niet besteld</option>";
if($row['status'] != "Besteld")
echo "<option>Besteld</option>";
if($row['status'] != "Onderweg naar hoofdlocatie")
echo "<option>Onderweg naar hoofdlocatie</option>";
if($row['status'] != "Onderweg naar vestiging")
echo "<option>Onderweg naar vestiging</option>";
if($row['status'] != "Ontvangen")
echo "<option>Ontvangen</option>";
echo "</select></td>";
echo "<td align='center' width='50px'><input name='checkbox[]' id='checkbox[]' type='checkbox' value='$ordernr'></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td></td><td></td><td></td><td></td>";
echo "<td><input type='submit' name='wijzigen' value='Wijzigingen Opslaan'/></td>";
echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
$statuses = $_POST['status'];
$delete = $_POST['delete'];
$del_id = $_POST['checkbox'];
if (isset($_POST['wijzigen'])) {
foreach($statuses as $ordernr => $status)
{
if($status != "")
$dbupdate = "UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'";
$query = mysqli_query($con,$dbupdate);
header("refresh: 0;");
}
}
if (isset($_POST['verwijderen'])) {
foreach($del_id as $value){
$dbdelete = "DELETE FROM overzicht WHERE ordernr='".$value."'";
$query = mysqli_query($con,$dbdelete);
}
header("refresh: 0;");
}
mysqli_close($con);
?>
Maybe the table besteloverzicht doesn't exist?
Please replace
$so = $con->prepare("SELECT * FROM besteloverzicht");
with
$so = $con->prepare("SELECT * FROM besteloverzicht") OR die(mysqli_error());
That should give you a better idea of what's going wrong.
Wrap your prepare statement to produce an error in case it fails
if (!($so = $con->prepare("SELECT * FROM besteloverzicht"))) {
echo "Prepare failed: (" . $con->errno . ") " . $con->error;
}
This may give you a better insight
You can use mysqli_prepare($con, 'SELECT * FROM besteloverzicht');
Related
I have a table where all the values are selected from the database, but some cells are empty. How do i put a dropdown list inside that empty cell. The values from the dropdown must come from the database
This is my code:
<?php
include("css/style.php");
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
if (mysqli_num_rows($result_list) > 0) {
$dropdown_list = '<select>';
while ($row = mysqli_fetch_array($result_list)) {
unset($id, $name);
$id = $row['id'];
$name = $row['id'];
$dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';
}
$dropdown_list .= '</select>';
}
// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<form method="POST">';
echo "<table>";
echo "<tr>";
echo "<th>Norm id</th>";
echo "<th>Norm</th>";
echo "<th>Omschrijving</th>";
echo "<th>Clusteren</th>";
echo "<th>Ordenen</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ($row['orden_name']) {
$data_list = $row['id'];
} else {
$data_list = $dropdown_list;
}
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['cluster_name'] . "</td>";
echo "<td>" . $data_list . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" </input><form>';
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if(isset($_POST['submit']))
{
$sql = "INSERT INTO norm (orden_id) VALUES ('$data_list')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// Close connection
mysqli_close($link);
?>
you can check this code. when $row['cluster_name'] empty then generate dropdown and first create dropdown then check your data exit or not but not tested
<?php
include("css/style.php");
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Iamthebest1009", "dktp");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$dropdown_list = '';
$sql = "SELECT * FROM orden";
$result_list = mysqli_query($link, $sql);
if (mysqli_num_rows($result_list) > 0) {
$dropdown_list = '<select>';
while ($row = mysqli_fetch_array($result_list)) {
unset($id, $name);
$id = $row['id'];
$name = $row['orden_name'];
$dropdown_list .= '<option value="' . $id . '">' . $name . '</option>';
}
$dropdown_list .= '</select>';
}
// Attempt select query execution
$sql = "SELECT * FROM Norm LEFT JOIN Cluster ON norm.cluster_id = cluster.id LEFT JOIN Orden ON norm.orden_id = orden.id ORDER BY norm_name";
if ($result = mysqli_query($link, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "<th>Norm id</th>";
echo "<th>Norm</th>";
echo "<th>Omschrijving</th>";
echo "<th>Clusteren</th>";
echo "<th>Ordenen</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ($row['cluster_name']) {
$data_list = $row['cluster_name'];
} else {
$data_list = $dropdown_list;
}
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $data_list . "</td>";
echo "<td>" . $row['orden_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
every time I run this code below:
<?php
$servername = "******";
$username = "******";
$password = "*******";
$dbname = "*******";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date, point, reason, teacher, giver FROM penalty WHERE studentid=".$_GET['id'];
$result = $conn->query($sql);
if ($result["num_rows"] > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["dated"]."</td>";
echo "<td>".$row["point"]."</td>";
echo "<td>".$row["reason"]."</td>";
echo "<td>".$row["giver"]."</td>";
echo "<td>".$row["teacher"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else if ($result["num_rows"] = 0)
{
echo "it is 0!";
}
else
{
echo "Unknown Error!";
}
$conn->close();
?>
I get the error message that I have written like this:
Unknown Error!
So, I'm thinking that the code has some problems in
if ($result["num_rows"] > 0)
What is the problem in the code, and what should I do to solve it?
ps. I'm a newbie in PHP, feel free to answer. Thanks in advance.
To check number of rows return from query we use
$result = $conn->query($sql);
if($result->num_rows > 0)
{
// your code
}
Instead
if ($result["num_rows"] > 0)
Your whole code would be
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<td>dated</td>";
echo "<td>point</td>";
echo "<td>reason</td>";
echo "<td>giver</td>";
echo "<td>teacher</td>";
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["dated"] . "</td>";
echo "<td>" . $row["point"] . "</td>";
echo "<td>" . $row["reason"] . "</td>";
echo "<td>" . $row["giver"] . "</td>";
echo "<td>" . $row["teacher"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "it is 0!";
}
Read http://php.net/manual/en/mysqli-result.num-rows.php
Your code is open for sql injection
Read http://php.net/manual/en/mysqli.prepare.php Prepare statement in mysqli
I've just started with PHP and literally been stuck now for 5 hours straight trying to figure this out! I understand what's happening but cannot for the life of me find a fix anywhere D: Basically each row is displayed on the users browser. Beside each one is a 'Mark as complete' button. When this button is pressed is changes the value from 0 to 1. Problem is, it changes the value of 0 to 1 for every row D: Please help me it's a pain in the but now haha! Heres my code:
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
$sql = "UPDATE details SET Status=1";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>
You are missing some steps. Here is the full process as you want to do.
Just replace with your. I hope this will solve your problem.
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
$sql = "SELECT * FROM details WHERE Status = 0";
// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
// Put your primary key column name in the place of Id
echo "<input type='hidden' name='user_id' value='".$row['Id']."'>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
and complete.php
<?php
// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;
// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);
// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}
// sql to delete a record
// Put your primary key column name in the place of Id
$sql = "UPDATE details SET Status=1 WHERE Id='".$_POST['user_id']."' ";
if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}
mysqli_close($link)
?>
I have a table that has a column named result, the result is a variable that bank sent to me by post method... and the values are : 1, 2 ... so when the bank send to me 1 , I want show to the user its accepted, and if it was 2, show to the user its rejected... i need a if formulas that do the rest.
<?php
$id_get= $_POST['id_get'];
$trans_id = $_POST['trans_id'];
$servername = "localhost";
$username = "blah";
$password = "blah";
$dbname = "blah";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<?php
mysqli_query($conn, "SET NAMES 'utf8'");
$result = mysqli_query($conn,"SELECT id_get, trans_id, result FROM users");
echo "<table border='1' width='800' align='center'>
<tr>
<th>ID</th>
<th>transaction Result</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr align='center'>";
echo "<td width=10%>" . $row['id_get'] . "</td>";
echo "<td width=10%>" . $row['trans_id'] . "</td>";
echo "</tr>";
}
echo "</table>";?><br>
<div align="center">
<?php
mysqli_close($conn);
?>
use case in PHP
$resultname= $row['trans_get'];
switch($resultname){
case("1"):
echo "Accepted";
break;
case("2"):
echo "Rejected";
break;
default:
echo "N/A";
break;
}
So it should be something like:
while($row = mysqli_fetch_array($result))
{
$resultname= $row['trans_get'];
switch($resultname){
case("1"):
echo "Accepted";
break;
case("2"):
echo "Rejected";
break;
default:
echo "N/A";
break;
}
echo "<tr align='center'>";
echo "<td width=10%>" . $row['id_get']. "</td>";
echo "<td width=10%>" . $resultname . "</td>";
echo "</tr>";
}
You can check the value of $row['result'] inside the while($row = mysqli_fetch_array($result)) block and display the content of <td> accordingly
while($row = mysqli_fetch_array($result))
{
echo "<tr align='center'>";
echo "<td width=10%>" . $row['id_get'] . "</td>";
echo "<td width=10%>" . $row['trans_id'] . "</td>";
if ($row['result'] == '1') {
echo "<td width=10%>Accepted</td>";
}
else if ($row['result'] == '2') {
echo "<td width=10%>Rejected</td>";
}
else {
echo "<td width=10%>Unknown</td>";
}
echo "</tr>";
}
or alternatively you can change your query to this
$result = mysqli_query($conn,"SELECT id_get, trans_id, (CASE WHEN result = 1 THEN 'Accepted' WHEN result = 2 THEN 'Rejected' ELSE 'Unknown' END) AS result_description FROM users");
and simply display $row['result_description']
while($row = mysqli_fetch_array($result))
{
echo "<tr align='center'>";
echo "<td width=10%>" . $row['id_get'] . "</td>";
echo "<td width=10%>" . $row['trans_id'] . "</td>";
echo "<td width=10%>" . $row['result_description'] . "</td>";
echo "</tr>";
}
You can use a case statement:
SELECT id_get, trans_id,
(case when result = 1 then 'Accepted'
when result = 2 then 'Rejected'
else 'Unknown'
end) as ResultString
FROM users;
You can also create a reference table in the database and use a join. This is recommended for a real application, because it ensures that the same strings are used for any query.
I want to create a page where I have to show records of an specific id but it is showing me this error
Notice: Undefined variable: mysqli in line 82.
Fatal error: Call to a member function query() on a non-object in line 82.
this is PHP code of program.
$con=mysqli_connect("XXXX","XXXX","XXXX","XXXX");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$id = $_GET['id'];
$sql = "SELECT * FROM details WHERE cat_id = $id";
$result = mysqli_query($con,$sql); //line #82
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
$id = $row['cat_id'];
echo "<tr>";
echo "<td><a href='detail.php?id=$id' >" . $row['cat_name'] . "</a></td>";
echo "</tr>";
}
echo "<table>";
echo "<tr>";
echo "<th>name</th>";
echo "<th>address</th>";
echo "<th>phone</th>";
echo "<th>uan</th>";
echo "<th>location</th>";
echo "</tr>";
mysqli_close($con);
You are not loop through record correctly like this:
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM details WHERE cat_id = $id");
echo "<table>
<tr>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>uan</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['uan'] . "</td>";
echo "</tr>";
}
$con=mysqli_connect("XXXX","XXXX","XXXX","XXXX");if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}$id =$_GET['id'];
$sql = "SELECT * FROM details WHERE cat_id=$id";$result=mysqli_query($con,$sql); //line #82$row = mysqli_fetch_array($result);echo "";echo "";echo "name";echo"address";echo"phone";echo"uan";echo"location";echo"";while($row=mysqli_fetch_array($result))
{$id = $row['cat_id'];echo ""; echo "" . $row['cat_name'] . ""; echo "";
}
mysqli_close($con);