This question already has answers here:
How to print a MySQL database table in PHP using PDO [closed]
(2 answers)
How can I get an unknown username given an ID?
(2 answers)
Closed 2 years ago.
I have made dynamic links in PHP, to display individual bikes, using the bike ID in my website. This is my code that makes each link individual on my index page:
$bike_id = $bike["bike_id"];
echo '<a href="./item.php?id='.$bike_id.'"><input type="button" value="See More" />';
Now the issue I face is trying to display each bike item pages unique values.
for example, if you selected "Fast bike" on index.php, you'd be taken to a page that had "fast bikes" description and price.
In the database, I have the columns:
"Bike_name",
"Bike_Price",
"Bike_Description
and on each the bikes item page, I want to display these values, but I don't know how to do it.
Here is what I have so far but it's not working:
<?php
require_once(__DIR__.'/includes/db.php'); //connect to the database
$item_id = $_GET['id']; //get the ID of the item
$item_data = mysql_query("SELECT bike_name"); //get the value of the column bike_name
echo "<h2>".$item_data['bike_name']."</h2>"; //display the value of the column bike_name
?>
I was advsed to do this:
// Select item from database
// Store results in $item_data
// echo $item_data[`item_name`];
My connection (I'm not showing my passwords or usernmane)
try {
$Conn = new PDO("mysql:host=".$db_config['db_host'].";dbname=".$db_config['db_name'],$db_config['db_user'],$db_config['db_pass']);
$Conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$Conn->setAttribute(PDO::ATTR_PERSISTENT, true);
$Conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e) {
echo $e->getMessage();
exit();
}
Related
This question already has answers here:
PHP display image BLOB from MySQL [duplicate]
(2 answers)
How to retrieve images from MySQL database and display in an html tag
(4 answers)
Closed 4 years ago.
So, I want to make search results with image in PHP. For example, when you search for a product on a website, you get the search results with images, like this:
So I've created a database with one record only (type: BLOB):
Also, I have a PHP code for display images:
<?php
$server = mysqli_connect("localhost","root","") or die("Não consigo ligar à BD");
mysqli_select_db($server,"pesquisa") or die("Não encontro a BD");
$query = mysqli_query($server, "SELECT * FROM produtos");
while($row = mysqli_fetch_array($query)) {
echo '<img height="300" width="300" src="data:image;base64, '.$row[2].' ">';
}
mysqli_close($server);
?>
But when I run it on a browser, the result is this:
Is this code wrong?
This question already has answers here:
Resetting array pointer in PDO results
(7 answers)
Closed 3 years ago.
I am trying to create a select box that will display several times on the page, where the options are populated by my SQL query. I am able to get this to display once but when I try to create a second, identical select box, there are no options in the dropdown box. Here is what I have working:
<?php include_once "app/init.php";
$dataQuery = $db->prepare("
SELECT column FROM dataType");
$dataQuery->execute([]);
$dataTypes = $dataQuery->rowCount() ? $dataQuery : [];
?>
<div>
<select>
<?php foreach($dataTypes as $dataType): ?>
<option>
<?php echo $dataType['dataType']; ?>
</option>
<?php endforeach; ?>
</select>
</div>
But when I try to add a second select box, it it blank. I am creating a table that will have this select box on every row for the user to select a data type so I need to recreate this dynamically several times. I am new to php, so what is the best way to go about this?
My init.php to show using PDO:
<?php session_start();
$_SESSION['user']=1;
$db = new PDO ('mysql:dbname=myDB;host=localhost', 'root', 'root');
if(!isset($_SESSION['user'])) {
die('You are not signed in');
};
You can only loop through the PDO statement once, so you should put the result in a temporary array.
Instead of:
$dataTypes = $dataQuery->rowCount() ? $dataQuery : [];
Do
$dataTypes = $dataQuery->fetchAll(\PDO::FETCH_ASSOC);
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to create a products page in a admin panel, where the administrator can input their products. I am trying to add the product to the database and use a or die output if anything goes wrong. But it seems like every time I type in or die, I receive a error code before evening running the code (last part of code).
What is the reason for this? Please help.
<?php
// parse from data
if (isset($_POST['product_name'])) {
$pid = mysql_real_escape_string($_POST['thisID']);
$Product_Name = mysql_real_escape_string($_POST['Product_Name']);
$desc = mysql_real_escape_string($_POST['Product_Desc']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET product_name='$Product_Name'LIMIT 1");
$productMatch= mysql_mum_rows($sql);
if ($productMatch>0){
echo "Sorry you tried to place a duplicate product name";
exit();
}
//add products to database
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error())
<?php
//blocks gravs
?>
You forgot to terminate(;) your code in die part and you open again a php code but you forgot to close the part last php code.so to correct it might be like this.
<?php
//..Some of your code here
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error());
//blocks gravs
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL check if a table exists without throwing an exception
Basically I have my MySQL dbname = test and my table name = page.
I want to create a query using a php PDO to check if the table "page" exists in my db "test"
I've tried this but it doenst work.. it always tells me that it doesnt exists.. even when it does
if (array_search('pages',$db->query('show tables')->fetch()) !== false) { echo "the db exists";
} else { echo "the db doesnt exists";
// Create tableS
//$IDB->execute();
}
there is no predefined test for existing table in PDO, you must do :
$pdo = new PDO($dsn,$user,$pass,$options);
$results = $pdo->query('SHOW TABLE LIKE \'page\'');
if(count($results)>0){echo 'table exists';}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP / MYSQL Add button to column
Please correct any mistakes throughout this question - I am very new to both PHP and MYSQL.
My goal is to create a table, that I will display onto a web page that looks something like this:
I have done the following. Any help on where I go wrong is much appreciated.
Created a MYSQL Table named "CustomerInformation"
Added five columns to the table, identical to the five columns in the picture above; (id, name, email, is_admin, Action).
I made four $POST text boxes whose data will be passed into each column (other than the last one as I want an "action" button to appear there).
Below I will show my full code for which I used in order to populate a new row in my CustomerInformation table.
<?php
// Connect to the database
mysql_connect ("localhost","username","password") or die ('Error: ' . mysql_error());
echo "connected to database!";
mysql_select_db ("database");
// Create variables to retrieve the POST data
$ID= $_POST['textbox1'];
$C_ID= $_POST['textbox2'];
$Value= $_POST['textbox3'];
$Count= $_POST['textbox4'];
$action = ' "<input type="submit" name="AddRow" value="Add New Row" />"';
// Insert data into table
$query = "INSERT INTO CustomerInformation (ID,C_ID,Value,Count,Action)
VALUES(
'".$ID."', '".$C_ID."', '".$Value."', '".$Count."','".$action."')";
mysql_query($query) or die ('Error updating database');
echo "Database updated successfully!";
?>
The only problem occurs when I include the line: $action = ' "<input type="submit" name="AddRow" value="Add New Row" />"';
I am clearly butchering this line, and I would greatly appreciate any help at all on this one!
The answer to your question is quite simply mysql_real_escape_string. Apply it on each string variable before you intermingle it with the SQL command.
It's simpler however not to bother with the old mysql_ API. You can keep the query separate from the data and avoid the effort with:
$db = new PDO('mysql:host=hostname;dbname=db', 'username', 'pwd');
$db->prepare(" INSERT INTO CustomerInformation
(ID,C_ID,Value,Count,Action) VALUES (?,?,?,?,?) ")
->execute(array($ID, $C_ID, $Value, $Count, $action));