I need some help. I have been developing a form which updates a database. It was all working fine using the jQuery Mobile form and then suddenly it has all stopped working. I have stripped the form down the the bare minimum and still its not working. When I click on update the information is placed the the browser as shown below.
update_db.php?niamh=1
but the database is not updated. If I click on refresh it updates and displays success.
If I remove all the jQuery header links it all works ok, so this is a jQuery problem. Sadly this was all working ok a couple of hours ago. code below, can any one please help.
HTML form
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form action="update_db.php" method="GET">
<input type="checkbox" data-role="flipswitch" name="niamh" id="switch" value="1">
<input type="submit" data-inline="true" value="Update">
</form>
</body>
</html>
php update_db.php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="14Odiham"; // Mysql password
$db_name="heating"; // Database name
$tbl_name = "roomControl";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$niamh = (isset($_GET['niamh'])) ? 1 : 0;
// Insert data into mysql
$sql = "UPDATE $tbl_name SET niamh=$niamh WHERE id=1";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>
<?php
//close connection
mysql_close();
?>
Try:
<form action="update_db.php" method="GET" data-ajax="false">
Related
OS: Ubuntu, Apache2, mysql-server
I hosted one index.html on localhost in Apache2 server with one dc.php file
<!DOCTYPE html>
<head>
</head>
<body>
<form method="post" name="form1" action="dc.php">
Username <input type="text" name="username" id="username"/>
Password <input type="text" name="password" id="password"/>
<input type="submit" value="Submit">
</form>
<form method="post" name="form2" action="cancel.html">
</body>
and here is code for dc.php file
<?php
session_start();
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$mysqlPassword="mypass#"; // Mysql pass
$db_name="mydb1"; // Database name
$tbl_name="mytbl1"; // Table name
mysql_connect("$host", "$username","$mysqlPassword")or die("Cannot connect to MySQL database.");
mysql_select_db("$db_name")or die("MySQL database unavailable.");
$password=$_POST['username'];
$confirm=$_POST['password'];
if ($password != $confirm) {
header("location:error.html");
break;
}
mysql_query("INSERT INTO mytbl1(password, confirm) VALUES('$password', '$confirm');");
echo "Updating records... $password";
ob_end_flush();
?>
so when i clicking on submit button instead of executing php file, the browser stops with url
http://localhost/dc.php
with blank page, and the values are not inserted in mysql.
your action of form is dc.php so if you submit your form the page goes to dc.php , your problem because an error occurred in your code you can add this lines to first of your code to see what excatly make error [in dc.php]
error_reporting(E_ALL);
ini_set("display_errors", 1);
I am facing a strange issue.... I have created a PHP program using which I can update and delete records stored in MySQL table. The program has PHP-Session functionality for login-logout purpose. Now the issue is, when I am trying to edit records stored in database, few are updating fine but some are not updating at all. Rather it redirects me to index.php page. Initially I thought it was the session which is causing the issue, but when I removed the session code and tried to update the content, same thing happened and it redirects. The data is simple text with the some html formatting. Any idea... what could be the reason behind this...???
include "connection.php";
if($_POST['sub']=="Update"){
$upd=mysql_query("UPDATE table_3 SET title='".htmlentities($_POST['ttl'],ENT_QUOTES,'UTF-8')."', con='".htmlentities($_POST['con'],ENT_QUOTES,'UTF-8')."' WHERE mid='".$_POST['mid']."'");
if(mysql_affected_rows()!=-1){ echo "<script>alert ('!! Data Updated Successfully !!');</script>"; }
else{ echo "<script>alert ('!! Error Updating Data !!');</script>"; }
}
connection.php has this code -
mysql_connect("localhost","user_name","password") or die ("Unable to connect server bcoz ".mysql_error());
mysql_select_db("db_name") or die ("Unable to select database bcoz ".mysql_error());
Updating the same data directly on MySQL table works fine....
Here is the complete code [I trimmed out most of the styling tags, but the core code is same ] -
<?php
session_start();
if(!isset($_SESSION['user'])){ header("location:."); }
?>
<html>
<head>
<title>Edit - Update</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
<div>Logged In As: <b><?php echo $_SESSION['user']; ?></b><span class="logout"><b>Log Out</b></span>
</div>
<?php
include "connection.php";
if($_POST['sub']=="Update"){
$upd=mysql_query("UPDATE table_3 SET title='".htmlentities($_POST['ttl'],ENT_QUOTES,'UTF-8')."', con='".htmlentities($_POST['con'],ENT_QUOTES,'UTF-8')."' WHERE mid='".$_POST['mid']."'");
if(mysql_affected_rows()!=-1){ echo "<script>alert ('!! Module Updated Successfully !!');</script>"; }
else{ echo "<script>alert ('!! Error Updating Module Info !!');</script>"; }
}
//URL of the page is - http://url/edit.php?mid=49
$res=mysql_query("SELECT * FROM table_3 WHERE mid='".$_GET['mid']."'");
$row=mysql_fetch_row($res);
?>
<form name="eform" method="post" action="">
<table style="margin-left:10px;">
<tr><td><font color="#025A8D"><b>Title : </b></font></td><td><input type="text" name="ttl" value="<?php echo ucfirst($row[2]); ?>" size="75" /></td></tr>
<tr><td><font color="#025A8D"><b>Content : </b></font></td><td><textarea name="con" cols="72" rows="20"><?php echo html_entity_decode($row[3],ENT_QUOTES,'UTF-8'); ?></textarea></td></tr>
<tr><td> <input type="hidden" name="mid" value="<?php echo $_GET['mid']; ?>" /></td></tr>
<tr align="center"><td></td><td><input type="submit" name="sub" value="Update" /></td></tr>
</table>
</form>
</body>
</html>
Hi I'm brand new to PHP and I'm just doing a simple form to learn. It just contains an email address, I want to Validate the information and send it to the database. My problem is connecting it to a database. I've done a few tutorials but just leave myself confused.
Right now this is my homepage
reg.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Form</title>
</head>
<body>
<?php include("validation.php"); ?>
<form method="post" action="connect.php" name="form">
<ul id="errors">
<li><?php echo $err_email; ?></li>
</ul>
<div id="wrapper">
<div>Email</div>
<div class="input"><input type="text" name="email" value="<?php echo $val_email; ?>" /></div>
</div>
</form>
</body>
</html>
This is my validation.php file
<?php
if($_POST)
{
$email = $_POST['email'];
// Email
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
$val_email = $email;
}else{
$err_email = 'Please enter valid Email address.';
}
if((strlen($val_email)>0) ){
header("Location: reg");
}else{ }
}
?>
finally my connect file
<?php
$host="localhost";
$username="admin";
$password=""; // Mysql password
$db_name="davidtest"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("davidtest")or die("cannot select DB");
// Get values from form
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $users(email)VALUES('$email')";
$result=mysql_query($sql);
// close connection
mysql_close();
?>
INSERT INTO $users(email)VALUES('$email')
$users isn't a variable so will be empty when being placed into the table. I suspect you meant
$sql="INSERT INTO $tbl_name (email) VALUES ('$email')";
However you should never use unescaped user strings in queries since a malicious user could inject SQL into your query (read up on SQL Injection).
Also please be aware that the mysql_* series of functions is now deprecated, no longer maintained and will be removed in a future release of PHP. Consider mysqli or PDO.
Somehow I get my sql error while trying to update a record in my sql db
Here's my current code
html form:
<html>
<head>
<title>title here</title>
</head>
<body>
<form action="end.php" method="POST">
<input type="text" id="comment" name="comment" placeholder="Kommentar"><p>
<input type="submit" value="Stop arbejde">
</form>
</body>
</html>
end.php
<?php
$host="localhost";
$username="root";
$password="password";
$db_name="db";
$tbl_name="log";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$comment=$_POST['comment'];
$datetime=date("y/m/d H:i:s");
$editit=date("W/D");
$sql="UPDATE log SET end=$datetime, comment=$comment WHERE editid='$editit'";
$result=mysql_query($sql);
if($result){
echo "Successful<BR>";
}
else {
echo "Fejl";
}
mysql_close();
?>
What am I doing wrong since I get that error "Fejl" ?
string must be wrap with single quotes
$sql="UPDATE log SET end='$datetime', comment='$comment' WHERE editid='$editit'";
but your query is prone to SQL Injection. please take time to read the article below
How can I prevent SQL injection in PHP?
The variable names in $sql should be inside single inverted comas.
$sql="UPDATE log SET end='$datetime', comment='$comment' WHERE editid='$editit'";
And also, you should remove paragraph tag from html code in form tag.
Before reading, please note that I am very new to both PHP and MYSQL. I have created a table in my MYSQL database. I would now like to 'spit out' this table onto a page through PHP. This part I seem to be okay with. After outputting the tables data into an HTML table, I would like to output an HTML form onto my page. So, I now have a table followed by a form. This form will contain a few text boxes that, when submitted, will post the data used to insert a new row into the preexisting table noted above.
All of the above code is currently in a PHP file named 'display.php'.
My Issue:
If the form described above is posting back to my 'display.php' file, after inserting a new row and displaying the new table information, what is stopping my code from inserting another new row full of NULL data? I'm sure I did a less than decent job of explaining this scenario so I will post some code.
HTML / PHP
<html>
<head>
<title>Html and PHP</title>
</head>
<body>
<!-- Form -->
<form action="insertdata.php" method="post">
Username: <input type="text" name="username" >
Hardware ID: <input type="text" name="hardwareid" >
<input type="submit" >
</form>
<?php
// Connect to MYSQL
$con = mysql_connect("localhost","blah","private");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Select database
mysql_select_db("dbname", $con);
// Insert posted data into table
$sql="INSERT INTO tablename(
Username,
HardwareID)
VALUES
('$_POST[username]','$_POST[hardwareid]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record successfully added...";
mysql_close($con)
?>
</body>
</html>
Again, I am a complete beginner - and I understand this. I want to know, must the different parts of the above code be placed into multiple files? I don't want to have to go to a new address, which is why this is causing me so much confusion I'd say.
try some thing like this,
connection.php file
// Connect to MYSQL
$con = mysql_connect("localhost","blah","private");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Select database
mysql_select_db("dbname", $con);
display.php file
<html>
<head>
<title>Html and PHP</title>
</head>
<body>
<!-- Form -->
<form action="process.php" method="post">
Username: <input type="text" name="username" >
Hardware ID: <input type="text" name="hardwareid" >
<input type="submit" >
</form>
</body>
</html>
process.php file
include_once("ur_file_dir/connection.php");
if ((isset($_POST['username']) && isset($_POST['hardwareid'])) {
$sql="INSERT INTO tablename(
Username,
HardwareID)
VALUES
($_POST['username'],$_POST['hardwareid'])";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record successfully added...";
mysql_close($con)
}
You should validate your input, ie:
if (!empty($_POST['username'] && !empty($_POST['hardwareid']) {
// do your insert here
}
Also, you should be wary of allowing user input to be inserted directly into your query, as this leaves your open to SQL injections. A better way to do this is to use PDO and prepared statements:
http://php.net/manual/en/pdo.prepared-statements.php