I need help solving this issue. I have stored my pictures as blob in mysql database and when i try to run this code to retrieve data from the database, the pictures are displayed as punch of characters but the rest of data types are working fine. I think the browser does not know that the data is a picture. How can I convert the blob into pictures in this code.
<?php
include_once('db.php');
if(isset($_POST['personID'], $_POST['fName'], $_POST['lName'], $_POST['title'], $_POST['pic']))
{
$personID= $_POST['personID'];
$fName= $_POST['fName'];
$lName= $_POST['lName'];
$title= $_POST['title'];
$pic= $_POST['pic'];
if(mysql_query("INSERT INTO samidb.persons VALUES ('$personID','$fName', '$lName', '$title', '$pic')"))
echo "Successful Insertion!";
else
echo "Please try again";
}
$res = mysql_query("SELECT * FROM samidb.persons");
?>
<html>
<head>
<style type="text/css">
li { list-style-type: none; display: inline-block; padding: 10px; text-align: center;}
//li:hover { background-color: yellow; }
</style>
</head>
<body>
<form action="." method="POST">
person ID: <input type="text" name="personID"/><br />
First Name: <input type="text" name="fName"/><br />
Last Name: <input type="text" name="lName"/><br />
title: <input type="text" name="title"/><br />
pic: <input type="image" name="pic"/><br />
<input type="submit" value=" Enter "/>
</form>
<h1>List from database ..</h1>
<ul>
<?php
while( $row = mysql_fetch_array($res) )
echo "<li>$row[personID]. <li>$row[fName] <li>$row[pic]</li>
<li><a href='edit.php?edit=$row[personID]'>edit</a></li>
<li><a href='delete.php?del=$row[personID]'>delete</a></li><br />";
?>
</ul>
</body>
</html>
Most people just store the path to the image. It's easy, it puts the saving of the data in the file system, built for that sort of thing, it cuts down on the db size, etc.
However, you should be able to use it as an inline image, usually base 64 encoded, as mentioned here:
Embedding Base64 Images
FYI, I tested this out using this code:
<img src="data:image/png;base64,<?php echo base64_encode (file_get_contents('./screenshot.png')); ?>" />
You could probably do something like:
<img src="data:image/png;base64,<?php echo base64_encode ($row['pic']); ?>" />
Related
Simply lost... I have been looking online for the answer but I cannot see why my code won't post to the MySQL database I have on the WAMP server is have installed locally?
also it doesn't echo the results when I'm trying to test if it is getting anything from the form.
here is my code its a simple form using php to insert it into the database.
the database table has ID (Auto Increment), Fname, Lname, Barcode.
<!DOCTYPE html>
<HTML>
<HEAD>
<meta charset="UTF-8">
<TITLE>Testing Barcode Reader</TITLE>
<style type="text/css">
#body {margin: 0px; }
#pageTop {
height: 90px;
background: grey;
padding: 10px;
}
#pageMiddle {
padding: 20px;
height: 250px;
}
#pageBottom {
padding: 20px;
height: 90px;
}
</style>
<?php
if (isset($_POST['submit'])){
$f = $_POST['fname'];
$l = $_POST['Lname'];
$b = $_POST['barcode'];
$db_conx = mysqli_connect('localhost', 'root', 'password');
mysqli_select_db($db_conx,'barcode_test');
$sql = "INSERT INTO barcode (Fname, Lname, Barcode) VALUES ('$f','$l','$b')";
$query = mysqli_query($db_conx, $sql);
}
?>
</HEAD>
<BODY>
<div id="pageTop">
<h1>Testing Barcode Reader</h1>
</div>
<div id="pageMiddle">
<div>
<form action="form.php" method="post">
<div>First Name:</div>
<input name="fname" type="text" /></br></br>
<div>Last Name:</div>
<input name="Lname" type="text" /></br></br>
<div>Barcode:</div>
<input name="barcode" type="text" /></br></br>
<div>Submit Button:</div>
<input name="submit" type="submit" value="Submit"/>
</form>
</div>
</div>
<div id="pageBottom">form for testing barcode reader inputs into mysql database</div>
<?php echo($f);
?>
</BODY>
</HTML>
thanks for any advice you can offer because I'm beginning to believe there is a problem with my WAMP server setup, because even if I copy and paste a simple form from the internet with the database connection settings setup it still wont post or echo?
Is php even working on your server? The best way to test this is by making a file with this code and this code only in it:
<?php phpinfo(); ?>
If the above doesn't work php isn't working correctly on your server.
Also check if you are going to the site by using localhost/filename.php or something similar.
I am a customer service assistant and I wanted to make a simple form to check the price of products on the webpage, without having to load the whole homepage.
The website is www.homebase.co.uk, and the search URL is http://www.homebase.co.uk/en/homebaseuk/searchterm/.
I want to make a form where it will add the text entered in the form after /searchterm/ without the question mark.
EG if I type in 775199 and press submit/search it will navigate to http://www.homebase.co.uk/en/homebaseuk/searchterm/775199.
Thanks so much for your help all :)
I really appreciate it!
Assuming you are using PHP. I think what you want to do is this:
<?php
//THIS IS THE SAME PAGE WHERE YOUR SEARCH FORM EXISTS
$searchPage = "http://www.homebase.co.uk/en/homebaseuk/searchterm/";
$formAction = "./"; //FORM IS SUBMITTED BACK TO ITSELF...
if(isset($_POST['search_term']){
//BUILD THE REDIRECTION LINK BASED ON THE DEFAULT SEARCH PAGE: $searchPage . $_POST['search_term'])
//AND THEN REDIRECT TO THE REDIRECTION LINK
header("location: " . $searchPage . htmlspecialchars(trim($_POST['search_term'])) );
exit;
}
Your HTML Form might look something like this:
<form method="POST" action="<?php echo $formAction; ?>" >
<input type="text" name="search_term" id="search_term" value="" class="search_term" />
<input type="submit" name"submit" id="submit" class="submit" value="Search" />
</form>
Okay, so I solved the problem:
<style type="text/css">
.biga {
font-family: Arial, Helvetica, sans-serif;
font-size: 36px;
color: #F90;
background-color: #FFF;
border: medium solid #000;
}
}
.centerpoint {
text-align: center;
}
</style>
<title>Product Search</title>
<p>
<div class="centerpoint"> <span class="centerpoint">
<input name="prog_site" type="text" class="biga" id="prog_site" value="" />
<a href="http://"
onclick="this.href=(
'http://www.homebase.co.uk/en/homebaseuk/searchterm/' + document.getElementById('prog_site').value)"
target="_blank">
<input name="so_link" type="button" class="biga" value="Search Product">
</a>
</p>
</span></div>
This is the code I used. Thanks for all of your help!
I am having a problem allowing users (teachers) to have their own sections. A section will allow a teacher that registers access to only their students and questions posted by them. When a teacher registers, they will be given a unique number that allows them access to a new separate section.
I have one users table that is filled with students and teachers. I tried adding another column or table for students and teachers with the same section number but I have not been able to get it to work.
Also the id I am using in phpMyAdmin, I am using as a student id number. I tried to use it as a section number which took away the ability to recognize each students' answers in my database (since they are stored as an id). I hope this is clear and thank you all ahead of time.
Here is my users table in my database.
And here is my register.php code...it is deprecated as of now (working on switching it to mysqli later)
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" href="main.css">
</head>
<body class="login" style="margin-bottom: 100px; " >
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<span style="display: inline-block;"><h1 style="margin-left: 40px; ">Register</h1></span><span style="display: inline-block; padding-left: 165px; "><p>Back</p></span>
<link rel="stylesheet" href="main.css">
<form action="" method="post">
<ul>
<li>
<input type="text" name="username" class="box" size="25" placeholder="Username"><br>
</li>
<li>
<input type="password" name="password" class="box" size="25" placeholder="Password"><br>
</li>
<br><br>
<button type="submit" style="margin-bottom: 5px;" class="subbutton" name="submit">Register</button>
</li>
</ul>
</form>
<?php
if(isset($_POST["submit"])){
if(!empty($_POST['username']) && !empty($_POST['password'])) {
$username=$_POST['username'];
$password=$_POST['password'];
$con=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('project') or die("Cannot select database");
$query=mysql_query("SELECT * FROM users WHERE username='".$username."'");
$numrows=mysql_num_rows($query);
if($numrows==0)
{
$sql="INSERT INTO users VALUES('','$username','$password','','','','1','d')";
$result=mysql_query($sql);
if($result){
echo "Account successfully created!";
} else {
echo "Didn\'t work, try again";
}
} else {
echo "That user ID already exists! Please try again";
}
} else {
echo "All fields are required!";
}
}
?>
</body>
</html>
I have a session variable that's an array and is supposed to store different usernames. Upon a user trying to log in, the username is checked against the array to see if the name exists within the array. If it's not found within the array the user is re-directed to a registration page, where the user can enter in a username and password.
This page, upon accepting the username and password, is supposed to update the session array, so that the next time the user tries logging in he/she is redirected to a different page.
I am able to register, but think that each time I go back to my main page the usernames array is refreshed to contain 0 entries.
Any way I can make my array more persistent?
products.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Studen Project #6 - M.M.</title>
<link rel="stylesheet" href="mystyles.css" />
</head>
<body>
<h1>Product Listings</h1>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Username: <input type="text" name="username" /><br>
Password: <input type="password" name="password" /><br><br>
Enter a Quantity for Each Product<br><br>
Pencils: <input type="number" name="pencils" /><br>
Notebooks: <input type="number" name="notebooks" /><br>
Folders: <input type="number" name="folders" /><br><br>
<input type="submit" />
</form>
<h2>Dixon Ticonderoga Wood-Cased Pencils</h2>
<h3>$2.88</h3>
<img src="http://ecx.images-amazon.com/images/I/41OAcvBFqXL.jpg" alt="pencil" />
<p>The World's Best Pencil with an exclusive #2 HB graphite core formula provides extra smooth performance</p>
<h2>Five Star Stay-Put Pocket Folder</h2>
<h3>$5.49</h3>
<img src="http://ecx.images-amazon.com/images/I/71HaaqlhilL._SL1280_.jpg" alt="folder" />
<p>Durable plastic folder helps keep sheets protected and in one place; great for reports, projects, as a take-home folder and for storage</p>
<h2>Five Star Wirebound Notebook</h2>
<h3>$18.98</h3>
<img src="http://ecx.images-amazon.com/images/I/61NgdQwSjIL._SL1000_.jpg" alt="notebook" />
<p>Five-subject plastic cover notebook has 200 college-ruled, 11 x 8.5 inch, 3-hole punched sheets</p>
<?php
$usernames = array();
$_SESSION["usernames"];
$_SESSION["quantity_total"];
$_SESSION["username"];
$_SESSION["pencils"];
$_SESSION["folders"];
$_SESSION["notebooks"];
if($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["usernames"] = $usernames;
$_SESSION["username"] = $_POST["username"];
$_SESSION["pencils"] = $_POST["pencils"];
$_SESSION["folders"] = $_POST["folders"];
$_SESSION["notebooks"] = $_POST["notebooks"];
if(!in_array($_SESSION["username"], $_SESSION["usernames"])) {
header("Location:registration.php");
exit();
} else {
$_SESSION["quantity_total"] = $_SESSION["pencils"] * 2.88 +
$_SESSION["folders"] * 5.49 + $_SESSION["notebooks"] * 18.98;
header("Location:preview.php");
exit();
}
}
?>
</body>
</html>
registration.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Student Project #6 - M.M.</title>
<style>
body {
background-color: lightgreen;
margin: auto;
width: 75%;
text-align: center;
}
h1 {
color: blue;
text-decoration: underline;
}
img {
width: 100px;
height: 100px;
}
form {
padding: 5px;
background-color: lightblue;
font-weight: bold;
font-family: Arial;
}
</style>
</head>
<body>
<h1>Register Here!</h1>
<img src="http://0.media.dorkly.cvcdn.com/36/35/6603dc5a9292104b44c349b85b5aaf7a-5-crazy-fan-theories-that-make-total-sense.jpg"
alt="thumbsup"><br>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" />
</form>
<?php
if($_SERVER["REQUEST_METHOD"] === "POST") {
array_push($_SESSION["usernames"], $_POST["username"]);
header("Location: products.php");
}
?>
</body>
</html>
You might consider rethinking the logic behind storing the list of users/usernames and their properties in the session.
With time, sessions will get bigger and bigger and you're going to have more problems down the line.
Instead, store that information in a database and consult it when needed.
Relative to your issue, the problem you're having with the session array being reset after the data is submitted is caused by this:
#line 41 $usernames = array(); <--- variable set to an empty array
...
if($_SERVER["REQUEST_METHOD"] === "POST") {
#line 50 $_SESSION["usernames"] = $usernames; <---- session variable affected with an empty array
$_SESSION["username"] = $_POST["username"];
...
Hope it helps. Good luck
I have a website that allows multiple files to be uploaded using HTML5 multiple setting. I use a PHP script to validate the files and upload them. Using only PHP and HTML/CSS I would like to create a progress bar for the file (or files) being uploaded. How can I achieve this? I know PHP can use APC to get file upload info, but how can that be incorporated for multiple files being uploaded?
<html>
<head>
<style type="text/css" media="screen">
div#banner_left {
position: absolute;
top: 0%;
left: 0%;
width: auto;
}
div#banner_right {
float: right;
width: auto;
}
</style>
<link rel="stylesheet" type="text/css" href="/css/structure.css">
</head>
<?php
echo "<div id='banner_right'>Welcome ";
echo $_SESSION['myusername'];
echo "<br><form action='/logout.php'>";
echo " <input type='submit' value='Logout' style='width:216px; font-size: 15px;'>";
echo "</form>";
echo "</div><br><br>";
?>
<!-- FILE UPLOAD SCRIPT//-->
<?php
if(isset($_POST['submit']))
{
$subject_list = $_POST['subject_list'];
$document_type = $_POST['document_type'];
$uploaddir = "/var/www/rye-high-website/Rye High/uploads/$subject_list/$document_type";
$files=array();
$fdata=$_FILES['rye_file'];
if(is_array($fdata['name'])){
for($i=0;$i<count($fdata['name']);++$i){
$files[]=array(
'name' => $fdata['name'][$i],
'tmp_name' => $fdata['tmp_name'][$i],
);
}
}
else $files[]=$fdata;
foreach ($files as $file) {
// uploaded location of file is $file['tmp_name']
// original filename of file is $file['name']
$move_file = move_uploaded_file($file['tmp_name'], "$uploaddir/".$file['name']);
}
if ($subject_list == "None") {
echo '<br><div class="alert">Please Pick A Course Code</div>';
}
if ($document_type == "None"){
echo '<br><div class="alert">Please Pick A Document Type</div>';
}
if($move_file){
echo '<br><div class="info">File is valid, and was successfully uploaded to: $subject_list folder</div>';
}
else {
echo '<br><div class="alert">Upload Failed</div>';
echo $move_file;
}
}
?>
<!-- CONTINUE WITH HTTP FORM//-->
<body>
<div id="banner_left">
<img src="/Rye High/images & scripts/logo_ryerson.gif"/>
</div>
<br><br><div class="img">
<img src="/images/logo.png" />
</div><center>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
Choose Document Type:
<select name="document_type">
<option value="None">Pick Document Type</option>
<option value="Textbooks">Text Books</option>
<option value="Notes & Assignments">Assignment/Notes</option>
</select><br>
Choose Course Code:
<select name="subject_list">
<option value="None">Pick Course</option>
<option value="ACC 100">ACC 100</option>
<option value="ACC 406">ACC 406</option>
</select><br>
Choose file(s) to upload (Max 500MB): <input name="rye_file[]" type="file" id="multiple" multiple />
<input type="submit" name="submit" value="Upload" />
</form>
I appreciate your help,
P.S. I would prefer to avoid using javascript since I'm not familiar with it well enough. PHP and HTML/HTML5 preferred.
You save yourself a lot of struggle if you use javascript to solve the issue! Under is a link to a tutorial, maybe take the best from that one, and search for it on google.
http://www.w3bees.com/2013/10/file-upload-with-progress-bar.html
http://hayageek.com/jquery-multiple-file-upload/