PHP MSQL server syntax error - php

I get this error when trying to get this details page for a project to work. Its for school and I dont really understand PHP that well yet.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
Here is the code for that page.
<?php
require_once('connection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = $recordID";
$Shoe_Details = mysqli_query($conn, $query_Shoe_Details) or die(mysqli_error(($conn)));
$row_Shoe_Details= mysqli_fetch_assoc($Shoe_Details);
$totalRows_Shoe_Details = mysqli_num_rows($Shoe_Details);
?>
<!DOCTYPE html>
<html>
<head>
<title>details</title><?php include 'connection.php';?>
</head>
<body>
<p>Product Name: <?php echo $row_Shoe_Details['Product_Name']; ?></p>
<p><img src=
"images/%3C?php%20echo%20$row_Shoe_Details['Image_Name'];%20?%3E"></p>
<p>Description: <?php echo $row_Shoe_Details['Product_Description']; ?></p>
<p>Price: $<?php echo $row_Shoe_Details['Product_Price']; ?></p><?php
mysqli_free_result($Shoe_Details);
?>
</body>
</html>

change your query as, use single quotes
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = '$recordID'";
Also remove this <?php include 'connection.php';?>, No need to include again

Pl. prepare connection.php file in followng way like
$dbHost = '';
$dbUser = '';
$dbPass = '';
$dbName = '';
// setting up the web root and server root for
// this shopping cart application
$con=mysql_connect('','','');
if(!$con)
{
die('connection failed');
}
$db=mysql_select_db('',$con);
if(!$db)
{
die('db is not selected');
}
pass proper value in this syntax save it and in your code remove second line and test then and give feedback

Related

What is the best way to include php database data with HTML

I am currently working on a website, i am quite new to this. I have established connection to my database with the connection.php file, but it seems quite hard to style a PHP file.
So i want to know:
Is there a smarter way to make a dropdown with data from a database and how would i include and syle the website then if it is still a .php file?
My preference would be to create a good looking website with HTML & CSS. But i don't know how to include my dropdown with data from the database?
As it is right now all are in one file, so it looks like this
Thank you in advance
Index.php
<?php
ini_set("display_errors", "On");
error_reporting(E_ALL);
$host='database.****.us-east-1.rds.amazonaws.com';
$db = '****';
$port = 5432;
$username = 'postgres';
$password = '****';
try {
$conn = new PDO("pgsql:host=$host;port=$port;dbname=$db;user=$username;password=$password");
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Choose Category</h1>
<?php
$sql ="select * from products inner join category on category.category_id = products.category";
//Prepare the select statement.
$stmt = $conn->prepare($sql);
//Execute the statement.
$stmt->execute();
//Retrieve the rows using fetchAll.
$users = $stmt->fetchAll();
// show menu from dropdown
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['name']; ?></option>
<?php endforeach; ?>
</select>
</body>
</html>
There is a much simpler way but I am going to give an example:
Suppose you have a php file:
connection.php:
<?php
function connect(){
$con=mysqli_connect("localhost","root","","dbname");
if(!$con)
die("Could not connect");
}
return $con;
}
?>
Now another file called getAll.php
<?php
require("connection.php");
$con=connect();
$getAll="select * from employees";
$res=mysqli_query($con,$getAll);
while($row=mysqli_fetch_array($res)){
echo "<option value='$row[firstname]'>$row['firstname']</option>"
}
mysqli_close($con);
?>
Now where you want to display the dropdown list:
Suppose a file called display.php
<html>
<body>
<select name="employee">
<?php
require("getAll.php");
?>
</select>
</body>
</html>
This is a simple way to get data from database and display them.

Save text with Tinymce online editor in Mysql database and show the saved text on the website

I have another question about connecting to Mysql database.
I have a simple text document with Tinymce online text editor. Online I want to change the text with the editor, save it to Mysql database and then show the new text online. I have the following scripts and I get this error from the file doAddContents.php:
Warning: mysql_real_escape_string(): Access denied for user ''#'localhost' (using password: NO) in /../doAddContents.php on line 8
Warning: mysql_real_escape_string(): A link to the server could not be established in /../doAddContents.php on line 8
I have no idea what I'm doing wrong. Here are the scripts:
first the script to connect to the database:
db.php:
<?php
function doDB() {
global $mysqli;
//connect to server and select database
$mysqli = mysqli_connect("localhost", "name", "pass", "db-name");
//if the connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
?>
doAddContents.php file:
<?php
include("db.php");
doDB();
$h4_block = "Contents Saved!";
$elm1 = $_POST['elm1'];
$entity_elm1 = htmlentities($elm1);
$entity_elm1 = mysql_real_escape_string($entity_elm1);
$add_contents_sql = "UPDATE tinymce_contents SET `contents`=
'$entity_elm1', `modified`=now()";
$add_contents_res = mysqli_query($mysqli, $add_contents_sql)
or die(mysqli_error($mysqli));
//close connection to MySQL
mysqli_close($mysqli);
//create nice message for user
$display_block = "<p>The page has been successfully updated.</p>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div>
<h4><?php echo $h4_block; ?></h4>
<?php echo $display_block; ?>
View Page!
</div>
</body>
</html>
View.php file:
<?php
include("db.php");
doDB();
$h4_block = "View Page!";
$get_contents_sql = "SELECT * FROM tinymce_contents";
$get_contents_res = mysqli_query($mysqli, $get_contents_sql)
or die(mysqli_error($mysqli));
if ($get_contents_res = mysqli_query($mysqli, $get_contents_sql)) {
//fetch associative array
while ($row = mysqli_fetch_assoc($get_contents_res)) {
$id = $row['id'];
$contents = $row['contents'];
$modified = $row['modified'];
//Draw the results
$view_block ="<p>ID: ".$id."</p>";
$view_block .="<b>Contents</b>:".html_entity_decode($contents);
$view_block .="<b>Modified</b>:".$modified."<br/>";
}
}
//close connection to MySQL
mysqli_close($mysqli);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div>
<h4><?php echo $h4_block; ?></h4>
<?php echo $view_block; ?>
Back to Page Edit!
</div>
</body>
</html>
According to the documentation of mysql_real_escape_string. The function will try to create a mysql connection if there is no connection opened with mysql_connect().
http://php.net/manual/en/function.mysql-real-escape-string.php
You should not be using mysql_real_escape_string() but mysqli_real_escape_string()
http://php.net/manual/en/mysqli.real-escape-string.php

Displaying blank page when including php-mysql connection file

I am using Ubuntu 14.04 and I am using lamp. I had a more complex issue but I figured out what is causing the error and made a small php-mysql connection snippet. When the connection file is included into the php/html file the whole thing displays a blank page.
My connect.php code:
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "ecalendar";
$conn = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if(mysqli_connect_errno(){
echo "Error".mysqli_connect_errno();
}
?>
and my web page with connection file included:
<?php
include "connect.php";
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Testing</p>
<?php
$sql="SELECT * FROM events WHERE eventid=1";
$result = mysqli_query($sql);
?>
<ul>
<li><?php $row = mysqli_fetch_array($result);
echo $result;
?>
</li>
</body>
</html>
<?php
mysqli_close($conn);
?>
The following line in connect.php:
if(mysqli_connect_errno(){
Should be:
if(mysqli_connect_errno()){ // note the extra ')'
You were not properly enclosing your condition with ().

Why isn't my mysql query from python shell going through to the web hosting database

ok so I have the following code that I am running in the python shell:
import MySQLdb
db = MySQLdb.connect(host = "xxxx",user="xxxx"password="xxxx",db="xxxx")
cur = db.cursor()
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
Fairly sure the connection part is working, I'll get an error if I enter in the wrong value, or if I deny access to the database for my computer's IP address.
on the webhosting server, I have the following basic index.php file, which I have tested on a server on my computer, and I know works. when I go to the website domain, I get the following error: "Database query failed."
Any ideas why the MySQL query isn't working? My webhosting is Cpanel with godaddy.com, should I look for something else?
<?php
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "xxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); /*1*/
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
$query ="SELECT * FROM qqqq"; /*2*/
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>
<!DOCTYPE html PUBLIC >
<html lang="en">
<head>
<title></title>
</head>
<body>
<ul>
<?php /*3*/
while($subject = mysqli_fetch_assoc($result)){
?><li><?php echo $subject['asdf'];?></li>
<?php
}
?>
</ul>
<?php
mysqli_free_result($result); /*4*/
?>
</body>
</html>
<?php
mysqli_close($connection); /*5*/
?>
You should call db.commit() to have it complete. By default, autocommit is turned off.
You also have an error in your code. The SQL should be a string.
Shouldn't the cursor execute be calling a string? You don't have quotes around your sql statement.
This line without quotas is incorrect:
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
It should be
cur.execute("CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))")
So test in your database whether you really have table qqqq.
You could install SQL Buddy or phpMyAdmin.

The Connection is Successful but nogetting the data into website mysql

We are Trying to Connect php & mysql we got connection successful but we are not receiving the data any ideas please guide
Also we are using mysql and php
The Connection is Successful but nogetting the data into website
enter code here`Please let me know who to correct it<html>
<head>
<title> Welcome to PDM</title>
</head>
<body>
<div>
<centre>
Good For Checking The Prices
</centre>
<?php>`
$db_host = "localhost";
$db_username = "wikiacwj_price";
$db_pass = "";
$db_name = "wikiacwj_price";
mysql_connect("$db_host","$db_username","$db_pass") or die ("Please Try Again");
mysql_select_db("wikiacwj_price") or die ("no data");
$sql = mysql_query("SELECT * FROM price_comparsion where product_name='ok'");
//write the results
while ($row = mysql_fetch_array($sql)); {
echo $row['product_name'];}
?>
</body>
</html>
The Semicolon immediately after your while statement telling the while loop to do ... nothing :)
...
while ($row = mysql_fetch_array($sql)) {
echo $row['product_name'];
}
...
shoud do the trick.

Categories