i have a problem with my php and sql code. I trying to make code that when i enter some data that related with the database, the output should show 'zero' if the row user_designid equal to 0 and if vice versa it will show 'one'.
but the problem is both of output only show the first anwser ('zero').
Php file (co.php is my config)
<?php
require "co.php";
$link = mysqli_connect($h,$u,$p,$db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$q = "select * from tab_ss_user where User_name ='".$_POST['name']."'";
$result = mysqli_query($link,$q);
if($result){
echo "<table border='1' >
<tr>
<td align=center> <b>user Id No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Password</b></td>
<td align=center><b>responsibility</b></td></td>";
while($data = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td align=center><option>$data[0]</option></td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
//this is the problem
if($data['User_DesignID'] == 0 ){
echo "<td align=center> zero </td>";
}
else{
echo "<td align=center> one </td>";
}
echo "</tr>";
}
echo "</table>";
}
else{
echo ' Failed';
}
?>
that all thank you.
You are not referencing the field correctly See enter link description here
When using mysqli_fetch_row, the result is an associative array so $data['User_DesignID'] will not be referenced correctly.
please make sure your column name is "User_DesignID"
if($data['User_DesignID'] == 0 ){
(Posted on behalf of the OP).
Thanks to Jeffrey idea, I have found an answer (please refer to Jeffrey's answer on this page).
And also if you want to use column name instead please use mysqli_fetch_assoc instead of row.
Related
I have a webpage that displays cars from the first car in the table to the last car with a while loop.
I have the following columns: Make, Model, Price. In my syntax I have an anchor tag around the Make rows that links to the description page of the Make you click on.
I want my <h> tags to change to the Model of the corresponding Make.
I've spent over an hour trying to achieve this but all I could come up with is this:
<?php
$query = "SELECT Model FROM inventory;";
$Vtitle = $conn->query($query);
$Vtitle_ar = mysqli_fetch_assoc($Vtitle);
echo "<h1>".$Vtitle_ar['Model']."</h1>";
?>
This works to an extent.
Every anchor I click replaces the <h> tags with only the first result under the Model column in my database.
Here is my code for the the entire car inventory page
<?php
$query = "SELECT * FROM inventory;";
/* Try to query the database */
if ($result = $conn->query($query)) {
// Don't do anything if successful.
}
else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
// Create the table headers
echo "<table id='Grid' style='width: 80%'><tr>";
echo "<th style='width: 50px'>Make</th>";
echo "<th style='width: 50px'>Model</th>";
echo "<th style='width: 50px'>Asking Price</th>";
echo "</tr>\n";
$class = "odd"; // keep track of whether a row was even or odd, so we can style it later
// Loop through all the rows returned by the query, creating a table for each row
while ($result_ar = mysqli_fetch_assoc($result)) {
echo "<tr class=\"$class\">";
echo "<td><a href='viewcar.php?VIN=".$result_ar['VIN']."'>".$result_ar['Make']."<a></td>";
echo "<td>".$result_ar['Model']."</td>";
echo "<td>".$result_ar['ASKING_PRICE']."</td>";
echo "</td></tr>\n";
// if the last row was even, make the next one odd and vice-versa
if ($class=="odd") {
$class = "even";
}
else {
$class = "odd";
}
}
echo "</table>";
$conn->close();
?>
Does anyone how I can do this?
I'm new to programming and I'm trying to use this for an actual project I'm working on for a hair salon's website
Add a WHERE clause to the query.
$vin = $_GET['VIN'];
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = ?");
$stmt->bind_param("s", $vin);
$stmt->execute();
$stmt->bind_result($model);
$stmt->fetch();
echo "<h1>$model</h1>";
Though not a solution resolved with the use of a where clause as given by #Barmar whilst formatting the code I did find an error within the HTML which was not immediately obvious
The line echo "</td></tr>\n"; has an extra </td> which would break the flow of the html and can have detrimental effects. Also, // Don't do anything if successful. makes no sense - if there are results then process the recordset otherwise show the error ;-)
<?php
$query = "SELECT * FROM inventory;";
if ( $result = $conn->query( $query ) ) {
echo "
<table id='Grid' style='width: 80%'>
<tr>
<th style='width: 50px'>Make</th>
<th style='width: 50px'>Model</th>
<th style='width: 50px'>Asking Price</th>
</tr>";
$i=0;
while( $result_ar = mysqli_fetch_assoc( $result ) ) {
$class = $i %2 == 0 ? 'even' : 'odd';
echo "
<tr class='$class'>
<td><a href='viewcar.php?VIN={$result_ar['VIN']}'>{$result_ar['Make']}<a></td>
<td>{$result_ar['Model']}</td>
<td>{$result_ar['ASKING_PRICE']}</td>
</tr>";
$i++;
}
echo "</table>";
} else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
$conn->close();
?>
For styling alternate table rows ( the above uses a modulus function to calculate odd/even ) you can do it with some simple CSS - such as
tr:nth-child( odd ){/* rules */}
I have a table in database name Accounts in which i have many rows and many columns, i want to show a column Account (all values) in my html table. I have tried many method to show a specific column values without using index in html using php and I am using MySql.
$storeArray = Array();
while($rowval = mysql_fetch_array($whole, MYSQL_ASSOC))
{
$storeArray[] = $rowval['Account'];
$status= $rowval['status'];
$ph1= $rowval['Phone1'];
$ph2= $rowval['Phone2'];
}
by using <?php echo $storeArray[0]; ?> and <?php echo $storeArray[1]; ?> in <td> i got the solution. My question is there any way, it automatically show all values without providing any index?
$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select username from User");
echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($username)=$rows->fetch_row()){
echo "<tr><td>$username</td></tr>";
}
echo "</table>";
This is a pretty elaborate question. I think the best I can do is point you in the right direction. There is a good tutorial for this on w3schools.com. You should at least read these:
PHP - Connect to MySQL
PHP - Select Data From MySQL
and maybe
PHP - Limit Data Selections From MySQL
**file user.php**
<?php
$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select id,username from User");
echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($id,$username)=$rows->fetch_row()){
echo "<tr>";
echo "<td>";
echo "<form action='user_details.php' target='_blank' method='post'>";
echo "<input type='hidden' name='txtId' value='$id' />";
echo "$id - $username";
echo "<input type='submit' name='btnView' value='View' />";
echo "</form>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
**file user_details.php**
<?php
if(isset($_POST["btnView"])){
$id=$_POST["txtId"];
$conn=new mysqli("localhost","root","","your_db");
$row=$conn->query("select id,username,email,phone from User where id='$id'");
list($id,$username,$email,$phone)=$row->fetch_row();
echo $id," ",$username," ",$email," ",$phone;
}
?>
if you want to fetch more than one database column in your html table column.
$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select col1,col2 from Tablename");
echo "<table border='1'>";
echo "<tr><th>col1</th><th>col2</th></tr>";
while(list($col1, $col2)=$rows->fetch_row())
{
echo "<tr><td>$col1</td><td>$col2</tr>";
}
echo "</table>";
I am unable to delete any record from database. I cannot find any error in this.
I have deleted records from another table by just changing a little bit but here it's not working. Below is the code to apply the delete query.The name of table is from where I want to delete records but its not happening here.
deleteSupplier.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$id1 = $_GET['id1'];
$query0 = "DELETE FROM tbl_supplier WHERE sup_id='$id1'";
if(mysql_query($query0)){
echo "<script>window.open('supplier_connect.php','_self')</script>";
}
else{
echo "Not deleted";
}
?>
This is the file where I fetch data from database and have delete button against each record. When I click on the button it does not delete record and show the error message. I can't find any error I think there is a logical error in this code. Please help.
supplier_connect.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$query = "Select * from tbl_supplier";
$run = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Supplier Id</th>
<th>Name</th>
<th>Contact Number</th>
<th>Quotation </th>
<th>Remove</th>
</tr>";
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['sup_id'] . "</td>";
echo "<td>" . $row['sup_name'] . "</td>";
echo "<td>" . $row['sup_contact'] . "</td>";
echo "<td>" .$row['sup_quotation']. "</th>";
echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>
You probably have the ID of the supplier referencing to another table, and upon DELETE, no action is set. You need to set the option 'upon DELETE: Cascade or SET NULL.' I'd go for SET NULL, else your whole reference record will be deleted as well.
EDIT: check the relationship between your contacts and quotations, see where your supplierid is going to.
use php variable in name of mysql create table
I want to use a PHP variable ( $userAltId ) as the name of the SQL table I am creating. However, despite looking at several examples, I have been unable to successfully create a table with this PHP variable. Using echo I have shown that the variable ( $userAltId ) does contain the predicted value, and if I plug in that value instead of the variable I can successfully create the table.
My latest attempt at the code is below, notice I use $useAltId three times, from creating, inserting, and selecting. Thanks for the help!
<?php
$con=mysqli_connect('localhost', 'alexpnqo_johnny', '?KlI+TtfNEIO', 'alexpnqo_johnny');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();
$userAltId = $_SESSION['userAltId'];//I am passing $userAltId from another php file
// escape variables for security
$FBname = mysqli_real_escape_string($con, $_POST['FBname']);
echo $userAltId;//echos desired value
$sqlFriendList="INSERT INTO '$userAltId' (FacebookFriend)
VALUES ('$FBname')";
if (mysqli_query($con,$sql)) {
echo "Table created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);";
}
echo "You have added ";
echo $FBname;
echo " to your friend's list!";
echo "<br>";
echo "Below is your friends list";
echo "<br>";
echo "<br>";
$result = mysqli_query($con, "SELECT FacebookFriend FROM '$userAltId'");
echo "<table border='1'>
<tr>
<th>Friends List</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FacebookFriend'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Use backticks $userAltId to all your queries (table name) so that it'll read your variable even spaces.
$sqlFriendList="INSERT INTO `$userAltId` (FacebookFriend) VALUES ('$FBname')";
I'm trying to make it so that when a users clicks one of the rows,it will take them to a new page whose link is given as the value of the row they selected and then retrieve the value with $_GET["timesub"].
Anyone know how to do this?
mysql_select_db("RRRC", $con);
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum=$loc");
echo "<table border='1'>
<tr>
<th> Submitted </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";
Assuming that $row['timesub'] identifies a row in your data set (I doubt it), just fix your echo instruction as:
echo "<td>" . $row['timesub'] . "</td>";
Escaping the html quotes properly.
echo "<td><a href='roomdata.php?timestamp=".$row['timesub']."'>".$row['timesub']."</a></td>";
Close the outer " before the . concatenator, replace the inner " with '
A good practice is to use the row's primary key to reference your get query; but yes - this can be done.
All you have to do is store the get data into a sanitized variable, and perform the required SQL lookup / data display.
EX:
$roomnum=mysql_real_escape_string(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['roomnum']));
Now, given that "roomnum" is your primary key just look it up and display:
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum='$roomnum'");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";