I've been trying to display the image I uploaded into the db using a simple insertform. I've only been able to retrieve the image-name into the "data-file", what I'm wondering is how can I easily display the image in the same form location instead of the image-name?
This is my result at this moment, as you can see I've only added an image to one of the inserts, and I would like to change the "footer.jpg-name" with the image displayed here.
<html>
<head>
</head>
<body>
<form action="insertform.php" method="post">
Topic: <input type="text" name="topic"><br />
Name: <input type="text" name="name"><br />
Attendance: <input type="text" name="attendance"><br />
Image: <input type="file" name="image"><br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","username","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("testingisfun",$con);
$sql = "INSERT INTO Lectures (Topic,Name,Attendance,Image) VALUES ('$_POST[topic]', '$_POST[name]', '$_POST[attendance]', '$_POST[image]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
</body>
</html>
Heres the "data-file".
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("testingisfun",$con);
$sql = "SELECT * FROM lectures";
$myData = mysql_query($sql,$con);
echo "<table border = 1>
<tr>
<th>Topic</th>
<th>Name</th>
<th>Attendance</th>
<th>Image</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['Topic'] . "</td>";
echo "<td>" . $record['Name'] . "</td>";
echo "<td>" . $record['Attendance'] . "</td>";
echo "<td>" . $record['Image'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Thanks for any sort of feedback!
Try this
echo "<td><img src='/{$record['Image']}'>" . $record['Image'] . "</td>";
I took this answer from linked answer that I had put in comments. There are other alternatives also provided for this problem.
echo "<td>" . $record['Image'] . "</td>";
replace with
echo "<td><img src=\"data:image/jpeg;base64,"" . base64_encode( $record['Image'] ) . "/></td>";
You are storing images in DB and while fetching it is giving you image data that is raw so to display images from data you can use this method.
This wont help in caching but it will solve the problem.
Related
I'm working with an ODBC connection rather than MySQL. I have a search function which I have more or less copied over to apply with my ODBC connection, however they are not working. Here is my code except for the connection:
<!doctype html>
<html>
<title> Quoting System </title>
<head>
</head>
<body>
<form class="form" method="POST" action='try31.php'>
Quote Number <input class="form-control" type="text" name="quote" id="quote" placeholder="Enter Quote Number">
<br>   <input class="btn btn-default" type="submit" name="search" value="Search">
</form>
<table>
<tr>
<th>Company Name</th>
<th>Address1</th>
<th>Address2</th>
</tr>
<?php
if (!$conn){
if (phpversion() < '4.0'){
exit("Connection Failed: . $php_errormsg" );
}
else{
exit("Connection Failed:" . odbc_errormsg() );
}
}
if(isset($_POST['search'])){
$quote = $_POST['quote'];
$query = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
}
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
// Disconnect the database from the database handle.
//odbc_close($conn);
?>
</table>
</body>
</html>
As I don't get an error message, I know my connection is working, however currently, when I select the button, data does not appear as expected... Please help! Thanks
Found that with MS SQL ODBC connection the query syntax is different than MySQL. I changed where I was calling my columns in the table from:
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
To this:
$result = odbc_exec($conn, $stmt);
while (odbc_fetch_row($result)) // while there are rows
{
echo "<tr>";
echo "<td>" . odbc_result($result, "CompanyName") . "</td>";
echo "<td>" . odbc_result($result, "Address1") . "</td>";
echo "</tr>";
}
The odbc_result function was crucial here.
<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if(!isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM pleasework ORDER BY ID");
$row = mysql_fetch_array($result);
}
?>
<form action="?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
<img id="close1" src="X.png" width="25" height="25" onclick ="div_hide1()">
<h2><font size="6">Please change existing data</font></h2>
<hr>
<br>
<font color="yellow">Change Name to: </font><input type="text" name="New" value="<?php echo $row['Name'];?>"/><br><br>
<font color="yellow"> Change Cause to: </font> <input type="text" name="New1" value="<?php echo $row['Cause'];?>"/><br><br>
<font color="yellow">Change Symptom to: </font><input type="text" name="New2" value="<?php echo $row['Symptom'];?>"/><br><br>
<font color="yellow"> Change Gene_affected to: </font><input type="text" name="New3"value="<?php echo $row['Gene_affected'];?>" /><br><br>
<input type="hidden" name="id" value="<?php echo $_GET['ID'];?>"/>
<input type="submit" onclick="clicked(event)" />
</form>
<?php
if(isset($_POST['submit'])){
mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[id]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
}
else {}
?>
This is my php file.
while($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . " </TD>";
echo "<TD>" . $row['Cause'] . " </TD>";
echo "<TD>" . $row['Symptom']. " </TD>";
echo "<TD>" . $row['Gene_affected'] . " </TD>";
echo "<TD><font color='red'>Delete row</font> </TD>";
echo "<TD><font color='red'>modify</font> </TD>";
echo "</TR>";
}
And this is the section which has a modify button that links to the edit.php file. The error here is that is doesnt bring over the values in the table to the editing page and then submitting the form doesnt work too. help please
Your code appears a bit confused.
First of all, why to put the modify routine after output the form? Especially since after modify you send the header function, that fails if previously there are some output.
Note also a typo: you forgot to properly open the php tag in the form declaration. Change-it in this way:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
The main problem is that you check if the $_POST[submit] if set, but this is not set, due to the absence of attribute name.
Change it in this way:
<input type="submit" name="submit" onclick="clicked(event)" />
Now your script should work (I don't have tested the sql).
Please also note that your UPDATE routine is redundant: you can reduce the 4 statement to only one in this way:
$result = mysql_query
(
"UPDATE pleasework SET Name='{$_POST[New]}', Cause='{$_POST[New1]}', Symptom='{$_POST[New2]}', Gene_affected='{$_POST[New3]}' WHERE ID={$_POST[id]}"
);
About PHP Original MySQL API:
This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
NOTE: mysql_* deprecated, so try to use PDO or mysqli_*.
Simple way:
<?php
if(isset($_POST['submit'])){
$result = mysql_query("UPDATE pleasework
SET Name='".$_POST['New']."',
Cause='".$_POST['New1']."',
Symptom='".$_POST['New2']."',
Gene_affected='".$_POST['New3']."'
WHERE ID=".$_POST['id'].");
if($result ){
echo "Change Successful<br>" ;
header("Location: databse.php");
}
mysql_close($con);
}
YOUR PHP:
while($row = mysql_fetch_array($result))
{ $spaces = " ";
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . $spaces."</TD>";
echo "<TD>" . $row['Cause'] . $spaces."</TD>";
echo "<TD>" . $row['Symptom']. $spaces."</TD>";
echo "<TD>" . $row['Gene_affected'] . $spaces."</TD>";
echo "<TD><a href='delete.php?id=".$row['ID'] ."'>";
echo "<font color='red'>Delete row</font></a>".$spaces."</TD>";
echo "<TD><a href='edit.php?id=" . $row['ID'] ."'>";
echo "<font color='red'>modify</font></a>".$spaces."</TD>";
echo "</TR>";
}
I am new to PHP and I made a simple program where you can apply your name and age, it will take the data to the database and the table will be added with a new row.
I want to add a new column where you can click "change", only the data from that particular row will show up in a few textboxes and can be changed. when pressing submit I want to use the UPDATE function to update the records.
example/plot:
Mike Towards 23 Change
Tyler Frankenstein 24
Change Sophie Baker 22
Change
I want to change the age of Sophie Baker to 24 so I press Change on that row.
Now I only want to get the data from that row and make some changes.
The code I have this far:
Drawing the table above the input fields and the input:
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<html>
<body>
<br />
<form action="insert.php" method="post"><br />
<input type="text" name="firstname"> Firstname <br />
<input type="text" name="lastname"> Lastname <br />
<input type="text" name="age"> Age
<p><input type="submit"></p>
</form>
</body>
</html>
Parser:
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added to the database";
echo "<p><a href=sql2.php>Back to form</a></p>";
mysqli_close($con);
?>
I have tried a few things, but I cant figure out how to show the content on the row I want to select.
Change the actual data with the update function won't be the problem, so I only need help to get the actual data from the correct row.
you'd need to select with the primary key of that table if any exists. if not you should create one. I assume you have a primary key named PersonID:
$query = "SELECT * FROM Persons WHERE PersonID = '" . ($_GET['PersonID']) . "'";
to add the edit button:
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th><th>Action</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td><a href = '?PersonID=" . $row['PersonID'] . "'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
I assume you have a column named "id".
you can do the following:
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// when you are in "edit mode" just display the row you will edit row
if (isset($_GET['id'])
$result = mysqli_query($con,"SELECT * FROM Persons where id = ".(int)$_GET['id']);
else
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td><a href='?id=" . $row['id'] . "'>change</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<html>
<body>
<br />
<form action="update.php" method="post"><br />
<input type="hidden" name="id" value="<?php echo isset($_GET['id']?$_GET['id']:'') ?>" />
<input type="text" name="firstname" value="<?php echo isset($row['FirstName'])?$row['FirstName']:'' ?>"/> Firstname <br />
<input type="text" name="lastname" value="<?php echo isset($row['LastName'])?$row['LastName']:'' ?>"/> Lastname <br />
<input type="text" name="age" value="<?php echo isset($row['Age'])?$row['Age']:'' ?>"/> Age
<p><input type="submit"></p>
</form>
</body>
</html>
update.php (handle both insertion and update):
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['id'])
$sql="UPDATE Persons set FirstName = ?, LastName = ?, Age = ?
WHERE id = ".(int)$_POST['id'];
else
$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES (?, ?, ?)";
$sth = mysqli_prepare($con, $sql);
$sth->bind_param($_POST[firstname],$_POST[lastname],$_POST[age]);
if (!$sth->execute())
{
die('Error: ' . mysqli_error($con));
}
echo "1 record ".(isset($_POST['id']?'modified':'added')." to the database";
echo "<p><a href=sql2.php>Back to form</a></p>";
THIS IS MY CODE HERE I AM GETTING PROBLEM THIS IS A INPUT TYPE TAG AND NOT SELECTED DATA FROM DATABASE USING PHP.
here is first part of this code is html formatted ad second part is php
i want select data from database to data slect to same page as we see on the sopping cart sites
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpSelect</title>
</head>
<body>
Insert Age for search
<form action="#" method="post" >
<input type="text" id="val" name="resValue" />
<input type="submit" value="submit" /></form>
<?php
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
echo $res;
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Try changing the SQL to this
SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'
First issue I found on your code is here:
<input type="submit" value="submit" />
it should be:
<input type="submit" value="submit" name="submit" />
To be able to get the results. Below are the codes:
<?php
$query = "";
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
$query = " where Age='$res'"
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons $query");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change your select query to this one.
SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."'
In your problem the all value of form get by the name of all fields of form.\
so here should be
<input type="submit" name="submit" value="submit"/>
Because in $_POST['submit'] submit is same as button's name.
$result = mysqli_query($con,"SELECT * FROM Persons where Age="25");
This is the last thing I'm gonna do, after I can make this work, I can produce to designing which might be a lot easier. Thanks for all your help.
I want to list a record from mysql using php by typing a query in the text box. Then the program will list all the records that has that element, for example address.
Here is my code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("koro", $con);
\\what will I put in the WHERE ADDRESS="?
$result = mysql_query("SELECT * FROM students WHERE ADDRESS=");
echo "<table border='1'>
<tr><th>Firstname</th>
<th>Lastname</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['IDNUMBER'] . "</td>";
echo "<td>" . $row['LNAME'] . "</td>";
echo "<td>" . $row['FNAME'] . "</td>";
echo "<td>" . $row['MNAME'] . "</td>";
echo "<td>" . $row['GRADEYR'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The listform.html:
<html>
<head>
<form action="list.php" method="post">
address:<input type="text" name="grup" value="" /><br/>
<input type="submit" name="List" value="" /><br/>
</form>
</head>
</html>
\what will I put in the WHERE ADDRESS="?thanks
You will find the value in the $_POST[] array.
$result = mysql_query("SELECT * FROM students WHERE ADDRESS = '{$_POST["grup"]}'");
This approach can be dangerous though - you'll want to also sanitize your $_POST[] data against SQL injection attacks - of which there are plenty of tutorials available.
$_POST['grup'] will contain the value in the field when posted.
Then do something like:
$result = mysql_query(sprintf("SELECT * FROM students WHERE ADDRESS='%s'", mysql_real_escape_string($_POST['grup'], $con)));
If you dont escape the POST value with sprintf and mysql_real_escape_string you can be targeted by SQL injection attacks.
http://en.wikipedia.org/wiki/SQL_injection