This is my second topic tonight, but it's only because I can't really find any helpful information in regards to updating data in SQL using php. For some reason this won't update. If anyone has any suggestions that would be great :), again sorry for asking another question, I'm just eager to learn from my mistakes.
HTML Section:
<html>
<head>
<title>Update Data</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="editphpscript.php" method="post">
ID To Update: <input type="text" name="id" required><br><br>
New Status: <select name="status" required>
<option>Status</option>
<option value="Assigned">Assigned</option>
<option value="Pending">Pending</option>
<option value="No_Signal">No Signal</option>
<option value="Installed">Installed</option>
</select>
<input type="submit" name="update" value="Update Data">
</form>
</body>
</html>
PHP Section
<?php
// php code to Update data from mysql database Table
if(isset($_POST['update']))
{
$hostname = "";
$username = "";
$password = "";
$databaseName = "";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// get values form input text and number
$id = $_POST['id'];
$status = $_POST['status'];
// mysql query to Update data
$query = "UPDATE `leadlist` SET `status`='".$status." WHERE `id` = $id";
$result = mysqli_query($connect, $query){
($result){
echo 'Data Updated';
}else{
echo 'Data Not Updated';
}
mysqli_close($connect);
}
?>
Related
This question already has answers here:
Are table names in MySQL case sensitive?
(5 answers)
Closed 2 years ago.
My database is connected because other input fields insert the data into a database table. However, one part of the code does not work for some reason although it does work on localhost. I am not sure why this is. Both tables are using the same connection file and variable. Here is the code for the one that does not work:
<?php
include 'config.php';
$name = $fileName = "";
$sql = "INSERT INTO output_Images(name, fileName, sub, author) VALUES (?,?,?,?)";
$sqll = "SELECT imageId FROM output_Images WHERE fileName = ?";
$fileSize = $_FILES['pdf_file']['size'];
if(isset($_SESSION['loggedin'])){
if(empty($_POST['title']) && empty($_POST['subject'])){
$err = "Input Worksheet Info <br><br>";
}else{
if(isset($_POST["submit"])){
if($stmt = mysqli_prepare($conn, $sqll)){
mysqli_stmt_bind_param($stmt, "s", $fileName);
$fileName = trim($_FILES["pdf_file"]["name"]);
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$err = "This file name is already taken. <br><br>";
} else{
$fileName = $_FILES['pdf_file']['name'];
$title = $_POST["title"];
$subject = $_POST["subject"];
$author = $_SESSION["username"];
}
}
if($stmt = mysqli_prepare($conn, $sql)){
mysqli_stmt_bind_param($stmt, "ssss", $title, $fileName, $subject, $author);
if(mysqli_stmt_execute($stmt)){
$err = "success";
}}
}
}
}
}
?>
Now here is the inputs that are taken into the create.php file:
<?php
include 'create.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<div class = "title">
<?php echo $_SESSION['err']; ?>
</div>
<form class = "main" method="POST" role="form" enctype="multipart/form-data"autocomplete="off">
<input type="file" name="pdf_file" id="pdf_file" class = "inputfile"accept="application/pdf"required><br><br>
<input type="text" name="title" placeholder = "Worksheet name" pattern="^[A-Za-z0-9 ]+$"maxlength = "28"required><br><br>
<select name="subject" required>
<option value="">Choose subject</option>
<option value="English">English</option>
<option value="History">History</option>
<option value="Math">Math</option>
<option value="Science">Science</option>
</select><br><br><br>
<button id="send" type="submit" name="submit" class="btn btn-success">Submit</button>
</form>
</body>
</html>
I found the answer. I am an idiot and I am sorry for wasting everyone's time, but apparently phpmyadmin tables are case sensitive when using online hosting services, but are not on localhost for some reason.
I'm building a simple app to send confirmation emails. I have a MySQL table with 4 columns: id, user_email, user_name and user_track. The form consists of a selectbox that shows all the user_email stored and two text inputs that should be autocompleted when an email is selected.
I have tried the following, but it just echo's all the entries for user_track and user_name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, user_email, user_name, user_track FROM demo_data ORDER BY id DESC";
$option = '';
$artist = '';
$track = '';
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$option .= '<option value = "'.$row['user_email'].'">'.$row['user_email'].'</option>';
$track .= '<input type="text" name="track" required value = "'.$row['user_track'].'">';
$artist .= '<input type="text" name="artist" required value = "'.$row['user_name'].'">';
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Admin</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main">
<h1>Accepted demo</h1>
<form action="demo.php" method="post">
<select name="select">
<?php echo $option; ?>
</select><br><br>
<?php echo $track; ?><br><br>
<?php echo $artist; ?><br><br>
<input type="date" name="date" placeholder="Release Date" required>
<br><br>
<input style="margin-top:10px;" class="btn" type="submit" value="Send email">
</form>
</body>
</html>
<?php
$conn->close();
?>
update From
$sql = "SELECT id, user_email, user_name, user_track FROM demo_data ORDER BY id DESC";
To
$sql = "SELECT id, user_email, user_name, user_track FROM demo_data WHERE user_email='".$_POST['selected_user_email']."' ORDER BY id DESC";
where selected_user_email is the name of input field that is being posted
PHP only adding Numbers to MySQL in column of VARCHAR instead of texts
when using query directly in MySQL it works...but if I use $_POST from HTML, IT fails
I don't know the reason how it is getting failed. what is the problem here ?
<?php
$link=mysqli_connect("localhost","root","","home_ac");
if(mysqli_connect_error()) {
die("error in database");
}
$name =$_POST["name"];
$query = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,$name)";
if(mysqli_query($link, $query)){
echo "done";
}
else {
echo "failed";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="text" placeholder="enter a name" name="name">
<input type="submit" value="add">
</form>
</body>
</html>
You need quotes around text
$query = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,'$name')";
Please, think about prepared query. It solve quotes problem and protect from SQL injection.
You have to use PHP Prepared Statements or PHP Data Objects (PDO).
For example, using PDO:
<html>
<head>
<meta charset="utf-8">
<title> Example PDO Insert </title>
</head>
<body>
<form method="post" action="" name="myForm" id="myForm">
<input type="text" placeholder="Enter Your Name" name="name" required="required">
<input type="submit" name="submit" value="add">
</form>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "home_ac";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ( isset($_POST['submit']) && !empty($_POST['name']) ) {
# code...
$sql = "INSERT INTO test (number,name) VALUES (NULL,'$name')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
For the past couple days, I have been trying to learn how to search an mysql database. So far I have the code below. for some reason it isn't searching and giving me results back. my database is named score and the table is all scores. Someone please help me with this.
It should be searching my database but it's coming up with no results. I have made sure everything is correct.
This file is searching.php
<?php
if (isset($_POST['search'])) {
$id = $_POST['id'];
$connect = mysqli_connect("localhost", "root", "root", "score");
$query = "SELECT `name` FROM `all_scores` WHERE `id` = $id LIMIT 1";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$name = $row['name'];
}
} else {
echo "Undifined ID";
$gameid = "";
}
mysqli_free_result($result);
mysqli_close($connect);
} else {
$gameid = "";
}
this is search.php
<!DOCTYPE html>
<html>
<head>
<title> PHP FIND DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="searching.php" method="post">
Id:<input type="text" name="id"><br><br>
<input type="submit" name="search" value="Find">
</form>
</body>
</html>
To get the form values inside the php file you need to use $_POST. Here's an example using PDO. You're only retrieving one row so you don't need the while loop.
searching.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $conn->prepare("SELECT `name` FROM `all_scores` WHERE `id` = :id LIMIT 1");
$q->bindValue(':id', $_POST['id'], PDO::PARAM_STR, 50);
$q->execute();
if ($q->rowCount() > 0) {
$check = $q->fetch(PDO::FETCH_ASSOC);
$row_id = $check['id'];
// do something
}
Html:
<form action="searching.php" method="post">
Id:<input type="text" name="id"><br><br>
<input type="submit" name="search" value="Find">
</form>
Take some time look at several other examples
I am working on PHP CRUD operations and I have created a basic edit form in PHP. I have not used any field validations and all I want is simply editing information.
I am following this tutorial
Once a user is clicked on Edit link he is directed to the following form on which the user is supposed to edit his data.
Here is the code
<?php
include_once './functions.php';
include_once './database.php';
function renderForm($firstName,$lastName,$age){
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="edit.php" method="post">
First Name<input type="text" name="firstname" value="<?php $firstName ;?>"><br/>
Last Name<input type="text" name="lastname" value="<?php $lastName ;?>"><br/>
Age<input type="text" name="age" value="<?php $age ;?>"><br/>
<input type="submit" name="submit" value="Edit">
Cancel
</form>
<?php
}
?>
<?php
if (isset($_POST['submit'])) {
$firstName = cleanData($_POST['firstname']);
$lastName = cleanData($_POST['lastname']);
$age = (int) $_POST['age'];
$id = $_GET['id'];
$query = "UPDATE basic ";
$query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
$query.="WHERE id=$id";
confirmQuery($query);
closeDatabase();
}else{
$id=cleanData($_GET['id']);
$query="SELECT * FROM basic WHERE id= {$id} ";
$result=confirmQuery($query);
$rows= mysqli_fetch_assoc($result);
$firstName=$rows['first_name'];
$lastName=$rows['last_name'];
$age=$rows['age'];
renderForm($firstName, $lastName, $age);
}
?>
</body>
</html>
//Additional information
//functions included in other files
function cleanData($input){
global $connection;
return mysqli_real_escape_string($connection,$input);
}
function confirmQuery($query){
global $connection;
$result=mysqli_query($connection, $query);
if(!$result){
return "Query failed : ".mysqli_error($connection);
}
else{
return $result;
}
}
function closeDatabase(){
global $connection;
mysqli_close($connection);
}
//I have not included the file which I am using to
//connect to the DB. I am sure there is no error with that file since it works
//properly with other php files
The problem that I have with my edit form is it does not show previously entered data and just shows only a blank form (similar to create form). (It does not happen when I run the demo in the above mentioned tutorial)
Netbenas IDE says variables which are inside HTML input tags seems to be unused in its scope. I have googled this question and found that warning can be simply ignored.
But Where have I gone wrong?
I am grateful to anyone who can kindly go through my code and show me the error.
Thank You :)
I have change your PHP code to below code use in your edit.php.if u get any issue put comment.
<?php
include_once './functions.php';
include_once './database.php';
if (isset($_POST['submit'])) {
$firstName = cleanData($_POST['firstname']);
$lastName = cleanData($_POST['lastname']);
$age = (int) $_POST['age'];
$id = $_GET['id'];
$query = "UPDATE basic ";
$query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
$query.="WHERE id=$id";
$r=mysql_query($query);
if($r)
{
echo "Record updated";
}
}
$id=$_GET['id'];
$query="SELECT * FROM basic WHERE id='$id' ";
$result=confirmQuery($query);
$rows= mysqli_fetch_assoc($result);
$firstName=$rows['first_name'];
$lastName=$rows['last_name'];
$age=$rows['age'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="edit.php" method="post">
First Name<input type="text" name="firstname" value="<?php echo $firstName ;?>"><br/>
Last Name<input type="text" name="lastname" value="<?php echo $lastName ;?>"><br/>
Age<input type="text" name="age" value="<?php echo $age ;?>"><br/>
<input type="submit" name="submit" value="Edit">
Cancel
</form>
</body>
</html>