I am trying to get a video play to play from a database. I have a form with the following code:
<form action="abs3xvideos.php" method="POST" enctype="multipart/form-data">
<input type="file" name="id" />
<input type="submit" name="submit" value="UPLOAD!" />
<form action="abs3xvideos.php">
Search ABS3X:
<input type="search" name="googlesearch">
<input type="submit">
</form>
I then have another page the form is linked to with the following code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('DB_Name', 'gaufensr_abs3x');
define('DB_User', 'gaufensr_owner');
define('DB_Password', 'password');
define('DB_Host', 'localhost');
$link = mysql_connect(DB_Host, DB_User, DB_Password);
if (!$link) {
die('could not connect:' . mysql_error());
}
$db_selected = mysql_select_db(DB_Name, $link);
if (!#db_selected) {
die('can\t use' . DB_Name. ': ' . mysql_error());
}
echo 'CONNECTED SUCCESSFULLY';
$id = $_POST['id'];
$value = $_POST['id'];
$sql = "INSERT INTO videos (video_name) VALUES ('$value')";
if (!mysql_query($sql)) {
die(`ERROR: ` .mysql_error());
}
if (isset($_POST['id'])){
$id = $_POST['id'];
$query = mysql_query("SELECT * FROM `videos` WHERE id='$id'");
while($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$video_name = $row['video_name'];
}
echo "You are watching " .$id. "<br />";
echo "<embed src=`$id` width='560' height='315'></embed>";
}
else
{
echo "Error!";
}
mysql_close();
?>
I get the following error message when I try to upload a video using the form page that I created:
CONNECTED SUCCESSFULLY
Notice: Undefined index: id in /home1/gaufensr/public_html/abs3xvideos.php on line 39
Notice: Undefined index: id in /home1/gaufensr/public_html/abs3xvideos.php on line 40
Error!
I am at a loss. I spoke with someone on stackflow earlier and they suggested that something might be wrong with my while loop but I am not to sure what the mistake could be. Should I separate the PHP code into different pages maybe?
Did you urlencode the name?
$video_name = urlencode($row['video_name']);
Or rawurldecode may work better.
$video_name = rawurldecode($row['video_name']);
you forget to close form
Use
<form action="abs3xvideos.php" method="post" enctype="multipart/form-data">
<input type="file" name="id" />
<input type="submit" name="submit" value="UPLOAD!" />
</form>//from close here
<form action="abs3xvideos.php">
Search ABS3X:
<input type="search" name="googlesearch">
<input type="submit">
</form>
Related
I am trying to make a form's input value the trigger for a database connection; can anyone tell me what I am doing wrong please?
I thought I could post the value and on the page load after submission use the posted value as a variable to complete my connect string.
Code:
<form name="form" action="Test.php" method="post">
<B>Please Enter Password</B> <input type='password' name='test-' id ='password123';/>
<button id='test'>Submit Year</button>
</form>
<?php
$password= $_POST['password123'];
$conn = #mysql_connect('localhost','root','$password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('ct', $conn);
?>
Try this
<?php
if ($_POST['do'] == 'something') {
$conn = mysql_connect('localhost','root','$password') or die(mysql_error());
mysql_select_db('ct', $conn);
//Uncomment the below to prevent refresh spam
//header("Location: {$_SERVER['REQUEST_URI']}");
//die();
}
?>
<form name="form" action="?process" method="POST">
<input type='hidden' name='do' value='something'/>
<strong>Please Enter Password</strong>
<input type='password' name='test-' id ='password123'/>
<button id='test'>Submit Year</button>
</form>
Consider using MySQLi also.
try this
<form name="form" action="<?php $_SERVER['PHP_SELF']?>" method="post">
<B>Please Enter Password</B> <input type='password' name='test-' id ='password123';/>
<button id='test' name="submit">Submit Year</button>
</form>
<?php
if(isset($_POST['submit'])) {
$password= $_POST['test-'];
$conn = #mysql_connect('localhost','root','$password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('ct', $conn);
}
?>
I have written a code for form which on submit opens the demo.php tag in the browser. I want the form to produce a (Thank you) alert on same page without redirecting to demo.php. Is it possible to embed a jquery notificaiton plugin? If yes then how?
<?php
error_reporting(0);
define('DB_NAME', 'form');
define('DB_USER', '******');
define('DB_PASSWORD', '*******');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo "<script type='text/javascript'>alert('It worked!')</script>";
exit();
$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];
$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
<form method="post" action="demo.php" id="form" >
<center><legend><h1> Enter Your Details </h1></legend>
<div class="formelement">
<label for="name">Name: </label>
<input type="text" name="name" id="name" required/>
</div>
<div class="formelement">
<label for="email">Email-ID: </label>
<input type="email" name="email" id="email" required/>
</div>
<div class="formelement">
<label for="bio">Your Message: </label>
<textarea rows="1" cols="15" name="bio" id="bio"></textarea>
</div>
<div class="formelement">
<input type="submit" value="SUBMIT" class="submit"/>
</div>
</form>
try changing
<form method="post" action="demo.php" id="form" >
to
<form method="post" action="" id="form" >
it will post form to same file and so do not redirect to other page
and print alert inside-
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo "<script type='text/javascript'>alert('It worked!')</script>";
$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['bio'];
$sql = "INSERT INTO form (name, email, bio) VALUES ('$value', '$value2','$value3')";
$result=mysql_query($sql) or die('Error: ' . mysql_error());
if ($result)
{
echo "<script type='text/javascript'>alert('success!')</script>";
}
mysql_close();
You can use jquery ajax form submission .so that you can give alert using jquery
see here
I've been trying to learn PHP and have been given a simple task to help me.
I'm trying to get a user to complete a form which has their email address in it, then save it to a database.
Here's my code so far:
<html>
<body>
<form action="postemail.php" method="post"> Email Address: <input type="text" name="emailaddress" /> <input type="submit" />
</form>
</body>
</html>
<?php
$connection = mysql_connect("localhost","edwardHost","password");
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_database", $connection);
$sql="INSERT INTO Subscribers (EmailAddress) VALUES ('$_POST[emailaddress]')";
if (!mysql_query($sql,$connection)) {
die('Error: ' . mysql_error());
}
mysql_close($connection);
?>
Thanks in advance!
Change your query to this
One more thing i forget last time you are missing single quete around $_POST[emailaddress]. In your query
$sql="INSERT INTO Subscribers (EmailAddress) VALUES ('".$_POST['emailaddress']."')";
Dont use mysl function as the are deprciated
Learn mysqli_ function or PDO Or both
Check this link for mysql identifier http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html
Try this example using PDO in your postemail.php
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', 'password');
try {
// create a new instance of a PDO connection
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// if the connection fails, display an error message
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['emailaddress']) && !empty($_POST['emailaddress'])) {
$emailaddress = $_POST['emailaddress'];
$sql = 'INSERT INTO Subscribers (EmailAddress) VALUES (:emailaddress )';
$stmt = $db->prepare($sql);
$stmt->bindValue('emailaddress ', $emailaddress);
$stmt->execute();
}
After you have totaly filled in the form, it first needs to check if the submit button is clicked, then it has to send it to a database.
You also need to give you submit button a name=""
HTML code:
<html>
<body>
<form action="postemail.php" method="post">
Email Address: <input type="text" name="emailaddress" />
<input type="submit" name="submit" value="add to database" />
</form>
</body>
</html>
PHP code:
<?php
if(isset($_POST['submit'])){
$connection = mysqli_connect("localhost","edwardHost","password","my_database");
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
$email = $_POST['emailaddress'];
$sql = "INSERT INTO Subscribers (EmailAddress) VALUES ('$email')";
if (!mysqli_query($connection,$sql)) {
die('Error: ' . mysql_error());
}
mysql_close($connection);
}
?>
<html> <body>
<form action="postemail.php" method="post">
Email Address: <input type="text" name="emailaddress" />
<input type="submit" />
</form>
</body> </html>
<?php $connection = mysql_connect("localhost","username","password");
if (!$connection) { die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_database", $connection);
$sql="INSERT INTO Subscribers (EmailAddress) VALUES ('$_POST[emailaddress]')";
if (!mysql_query($sql,$connection)) { die('Error: ' . mysql_error()); }
mysql_close($connection);
?>
I have a HTML form handled by php. The form has various text input fields and one of those asks the user to input a link containing http://.
The problem is when I submit a link like http://www.google.com/ the form fails badly by displaying random content from different php files on my website.
Here's a resume of my code:
<form action="add.php" method="post">
<input type="text" name="link" />
<input type="submit" value="submit" />
<?php
if (isset($_POST['link']))
{
$link = mysql_escape_string($_POST['link']);
mysql_query("INSERT INTO mytable(link) values($link)");
}
?>
Works just fine when not submitting a string that contains http://. Anyone has any idea why this happens?
Sidenote: You will find an mysqli_* based version further down.
Assuming you are connected to your DB, you did not wrap $link in VALUES with quotes.
Change this line:
mysql_query("INSERT INTO mytable(link) values( $link )");
^ ^ <- missing quotes
to:
mysql_query("INSERT INTO mytable(link) values('$link')");
Tested and working with this example: (form is set to self) and using http://www.google.com/ as an input and a few others containing http://
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASS', 'xxx');
define('DB_HOST', 'xxx');
$dbh = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$dbh)
{
die('Could not connect to database: ' . mysql_error());
}
$db_select = mysql_select_db(DB_NAME);
if(!$db_select)
{
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
if(isset($_POST['submit']) && !empty($_POST['link'])){
$link = mysql_real_escape_string($_POST['link']);
mysql_query("INSERT INTO mytable (link) values('$link')");
if(!mysql_query)
{
die("sorry");
}
else{ echo "Success"; }
mysql_close();
}
?>
<!DOCTYPE html>
<html>
<body>
<title></title>
<h3>Place your Link Below:</h3>
<form action="" method="post">
<input type="text" name="link" />
<input type="submit" name="submit" value="submit" />
</body>
</html>
Footnotes:
Do consider switching to mysqli_* functions with prepared statements or PDO. The mysql_* functions are deprecated and will be removed from future releases.
Here is an mysqli_* based version:
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASS', 'xxx');
define('DB_HOST', 'xxx');
$dbh = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
if(!$dbh)
{
die('Could not connect to database: ' . mysqli_error());
}
$db_select = mysqli_select_db($dbh,DB_NAME);
if(!$db_select)
{
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error());
}
if(isset($_POST['submit']) && !empty($_POST['link'])){
$link = mysqli_real_escape_string($dbh,$_POST['link']);
mysqli_query($dbh,"INSERT INTO mytable (link) values('$link')");
if(!mysqli_query)
{
die("sorry");
}
else{ echo "Success"; }
mysqli_close($dbh);
}
?>
<!DOCTYPE html>
<html>
<body>
<title>Home Page</title>
<h3>Please Place your Order Below:</h3>
<form action="" method="post">
<input type="text" name="link" />
<input type="submit" name="submit" value="submit" />
</body>
</html>
This question already has answers here:
How can I get a PHP value from an HTML form?
(2 answers)
Closed 1 year ago.
I am currently trying to complete a project where the specifications are to use a search form to search through a packaging database. The database has lots of variables ranging from Sizes, names, types and meats. I need to create a search form where users can search using a number of different searches (such as searching for a lid tray that is 50 cm long).
I have spent all day trying to create some PHP code that can search for info within a test database I created. I have had numerous amounts of errors ranging from mysql_fetch_array errors, boolean errors and now currently my latest error is that my table doesn't seem to exist. Although i can enter data into it (html and php pages where I can enter data), I don't know what is causing this and I have started again a few times now.
Can anyone give me some idea or tips of what I am going to have to do currently? Here is just my small tests at the moment before I move onto the actual sites SQL database.
Creation of database:
<body>
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE db_test", $con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_select_db("db_test", $con);
$sql = "CREATE TABLE Liam
(
Code varchar (30),
Description varchar (30),
Category varchar (30),
CutSize varchar (30),
)";
mysql_query($sql, $con);
mysql_close($con);
?>
</body>
HTML search form page:
<body>
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
The PHP code I am using to attempt to gather info from the database
(I have rewritten this a few times, this code also displays the "table.liam doesn't exist")
<body>
<?php
$con = mysql_connect ("localhost", "root", "");
mysql_select_db ("db_test", $con);
if (!$con)
{
die ("Could not connect: " . mysql_error());
}
$sql = mysql_query("SELECT * FROM Liam WHERE Description LIKE '%term%'") or die
(mysql_error());
while ($row = mysql_fetch_array($sql)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
mysql_close($con)
?>
</body>
try this out let me know what happens.
Form:
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
Form.php:
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
Edit: Cleaned it up a little more.
Final Cut (my test file):
<?php
$db_hostname = 'localhost';
$db_username = 'demo';
$db_password = 'demo';
$db_database = 'demo';
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
}
?>
</body>
</html>
You're getting errors 'table liam does not exist' because the table's name is Liam which is not the same as liam. MySQL table names are case sensitive.