PHP MYSQL Record not added [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 days ago.
Improve this question
i am creating simple registation using php mysql.when i click the save button record need to add into the database table.but couldn't i didn't get any error but record not addedd.
php isset method is not working.
Form Design
<form action="add.php" method="post">
<label>Name</label></br>
<input type="text" name="name" id="name" class="form-control"></br>
<label>Address</label></br>
<input type="text" name="address" id="address" class="form-control"></br>
<label>Mobile</label></br>
<input type="text" name="mobile" id="mobile" class="form-control"></br>
<input type="submit" value="Save" class="btn btn-success"></br>
</form>
Php Code
<?php
$conn = mysqli_connect("localhost","root","");
$db = mysqli_select_db($conn,"bcrud");
$errors = "";
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['mobile'];
if(! $errors)
{
if(mysqli_errno($conn))
{
echo "Errr " , mysqli_errno($conn);
}
$sql = "insert into register(name,address,mobile)values('$name','$address','$phone')";
if(mysqli_query($conn,$sql))
{
echo "addedd";
}
else
{
echo "Errr " , mysqli_error($conn);
}
}
}
?>

Related

PHP sql database not updating after update query, indexes are not defined? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am having an issue where I am not able to update the database. I think the error is in the update query itself, but I am new to SQL and PHP and therefore I am not 100% sure. Any help would be greatly appreciated.
I was getting an undefined index error which I fixed with but the issue still exists:
$id = $_GET["id"];
$listdate = isset($_POST['list_date']) ? $_POST['list_date'] : '';
$listprice = isset($_POST['list_price']) ? $_POST['list_price'] : '';
$solddate = isset($_POST['sold_date']) ? $_POST['sold_date'] : '';
$soldprice = isset($_POST['sold_price']) ? $_POST['sold_price'] : '';
$shipdate = isset($_POST['ship_date']) ? $_POST['ship_date'] : '';
$shipcost = isset($_POST['ship_cost']) ? $_POST['ship_cost'] : '';
After changing to the above code(which did not work) I am still having an issue updating the database. Here is the full current code:
<?
$id = $_GET["id"];
$listdate = $_POST["list_date"];
$listprice = $_POST["list_price"];
$solddate = $_POST["sold_date"];
$soldprice = $_POST["sold_price"];
$shipdate = $_POST["ship_date"];
$shipcost = $_POST["ship_cost"];
$servername = "localhost";
$username = "inventory";
$password = "*****";
$db = "products";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
$sql = "UPDATE inventory SET list_date = '$listdate', list_price = '$listprice', sold_date = '$solddate', sold_price = '$soldprice', ship_date = '$shipdate', ship_cost= '$shipcost' WHERE product_id = ' .$id. '";
if($conn->query($sql) === TRUE){
echo "Record Saved.";
} else {
echo "Error!";
}
$conn->close();
?>
Even when I change the ID manually to an assigned ID with the database, the updating still does not work and makes no changes in the database. I know the form does not look pretty, but I set it up for testing. Here is how I have the form set up to capture the changes:
<form action="/SulleySells/scripts/updateitem.php?id=<?php echo $id;?>" method="post">
<script type="text/javascript">
function ShowHideDiv(listed) {
var updatel = document.getElementById("updatel");
updatel.style.display = listed.checked ? "block" : "none";
}
function ShowHideDiv2(sold) {
var updates = document.getElementById("updates");
updates.style.display = sold.checked ? "block" : "none";
}
</script>
<label for="listed">
<input type="checkbox" id="listed" onclick="ShowHideDiv(this)" />
Listed?
</label>
<label for="sold">
<input type="checkbox" id="sold" onclick="ShowHideDiv2(this)" />
Sold?
</label>
<hr>
<div id="updatel" style="display: none">
<h3>Update Listing Details:</h3>
<label for="listdate">Listed Date:</label>
<input type="date" id="updateltext" name="listdate" value=""/>
<br>
<label for="listprice">Listed Price:</label>
<input type="text" id="updateltext" name="listprice" value=""/>
<br>
<button>Update</button>
<hr>
</div>
<div id="updates" style="display: none">
<h3>Update Sale Details:</h3>
<label for="solddate">Sold Date:</label>
<input type="date" id="updatestext" name="solddate" value=""/>
<br>
<label for="soldprice">Sold Price:</label>
<input type="text" id="updatestext" name="soldprice" value=""/>
<br>
<label for="shipdate">Ship Date:</label>
<input type="date" id="updatestext" name="shipdate" value=""/>
<br>
<label for="shipcost">Ship Cost:</label>
<input type="text" id="updatestext" name="shipcost"value=""/>
<br>
<button>Update Sold Info</button>
<hr>
</div>
</form>
At first you have to change:
<input type="date" id="updateltext" name="listdate" value=""/>
to:
<input type="date" id="updateltext" name="list_date" value=""/>
To match what you have in your php code and apply it to the rest of the html inputs
then change:
WHERE product_id = ' .$id. '
to:
WHERE product_id = '$id'
Notice
Don't forget to format for="" in lable with id="" in the input so as not to give you a different result

Phpmailer, errors when try to send [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
php is not my cup of tea, but unfortunately I need to create a mail form with attachment to my website.
I fallowed this tutorial https://www.youtube.com/watch?v=ydQT6Bt4zIk&t=33s, but got some errors. It's looks like that my input values are not decelerated.
I used ' $_POST['name of html input '];' to get it.
project:
http://mork.webd.pl/uploads/mail-form/mail-form.7z
list of errors:
Warning: Undefined array key "email" in C:\xampp2\htdocs\vomo\send.php on line 15
Warning: Undefined array key "email" in C:\xampp2\htdocs\vomo\send.php on line 57
PHPMailer\PHPMailer\Exception: Message body empty in C:\xampp2\htdocs\vomo\vendor\phpmailer\phpmailer\src\PHPMailer.php:1580 Stack trace: #0 C:\xampp2\htdocs\vomo\vendor\phpmailer\phpmailer\src\PHPMailer.php(1488): PHPMailer\PHPMailer\PHPMailer->preSend() #1 C:\xampp2\htdocs\vomo\send.php(58): PHPMailer\PHPMailer\PHPMailer->send() #2 {main}
my php code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$name = $_POST['name-of'];
$email = $_POST['email'];
$subject = $_POST['phone'];
$message = $_POST['mess-content'];
try {
$mail->SMTPDebug = 10;
$mail->isSMTP();
$mail->Mailer="smtp";
$mail->Host=gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->Username ='xowlasky#gmail.com';
$mail->Password="xxxx";
$mail->SMTPSecure = 'tls';
$mail->Port=465;
$mail->setFrom("xowlasky#gmail.com", $_POST['name-of']);
$mail->addAddress("jaroslaw.mor#gmail.com");
//attachment
if(array_key_exists('attachment',$_FILES)) {
$img_name = $_FILES['attachment']['name'];
$upload = tempnam(sys_get_temp_dir(), hash('sha256' , $_FILES['attachment']['name'] ));
$uploadFile = $_SERVER['DOCUMENT_ROOT'].'/Images/'.$img_name;
if(move_uploaded_file( $_FILES['attachment']['tmp-name'], $uploadFile)){
$mail->addAttachment($uploadFile, "My Attachment");
}
}
$mail -> Subject ='Zapytanie ze strony www';
$mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";
$mail->send();
header("Location: sent.php");
exit();
}
catch(Exception $e) {
echo $e;
}
my HTML code of form
<label for="fname">Imię i Nazwisko <span class="star">*</span></label>
<input type="text" id="name" name="name" placeholder="Podaj imię i nazwisko..." pattern="^([A-ZŻŹĆĄŚĘŁÓŃ][a-zżźćńółęąś{3,}]+)(\s|-|_)+([A-ZŻŹĆĄŚĘŁÓŃ][a-zżźćńółęąś]+)$" required>
<label for="fname">e-mail <span class="star">*</span></label>
<input type="mail" id="mailadress" name="mail" placeholder="wprowadź adres email..." pattern="^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$" required>
<label for="text">Telefon</label>
<div class="form-info">wprowadź numer telfonu w formie +xx xxx xxx xxx</div>
<input type="text" id="phone-number" name="phone" placeholder="podaj nr telefonu..." pattern="/^[+]?\d{1,3}\s?(-)?[0-9]{3}(\s)?[0-9]{3,4}(\s)?[0-9]{3}(\s)?$/">
<label for="subject">Treść wiadomości:<span class="star">*</span></label>
<div class="form-info">pole musi zawierać minimum 10 znaków</div>
<textarea id="message" name="mess-content" placeholder="Treść wiadomości..." style="height:300px" pattern=".{10,}" required></textarea>
<br>
'<div class="alert alert-success"></div>
<div class="error-message "></div>
<div class="buttons-container">
<input type="submit" id="send" value="Wyślij">
<input id="upload" name="attachment" id="upload-file" type="file" >
</div>
</form>
</div>
</div>
https://jsfiddle.net/jzxh937b/
Like Jeroen said, the name of your input email is not the same in your php code. Change your email input name into this.
<input type="mail" id="mailadress" name="email" placeholder="wprowadź adres email..." pattern="^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$" required>

I don't know what i did wrong on my code, the error message for php form validation stopped working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I don't know what I did wrong on my code, the error message for php form validation stopped working.
It was working perfectly until i added value attribute to the input so that the user input will persist even if the page refresh and didn't deliver due to typeError.
The form does'nt show any error again but my reason for adding the value attribute is working.
I'm learning php, please help me to understand why i'm having the issue.
I don't understand because i'm not getting any error from php.
This is my code
<?php
// empting the value variables when user have'nt typed anything to prevent error. This is the shorthand of typing samething that's going to have the same value
$email = $title = $ingredients = '';
// put out the error on the html instead of echoing it
// so i used array so that i can neatly put out all the errors instead of using different variables for all
$error = array('email' => '', 'title' => '', 'ingredients' => '');
// check if the form was clicked and retrive the values sent
// i will achieve this by using a default method called isset() and i will check if value is contained in the form using the submit btn, this is because when a user clicks on the form submit, the user have entered a value
if(isset($_POST['submit'])){
// check if the field submited is empty
// we achieve this using a default method called empty()
// we check them one field at a time
// check for email
if(empty($_POST['email'])){
$error['email'] = ' Email is empty';
} else {
$email = $_POST['email'];
}
// check for title
if(empty($_POST['title'])){
$error['title'] = ' Title is empty';
} else {
$title = $_POST['title'];
}
// check for ingredients
if(empty($_POST['ingredients'])){
$error['ingredients'] = ' Ingredients is empty';
} else {
$ingredients = $_POST['ingredients'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include 'template/header.php'?>
<form action="form.php" method="POST">
<div class="input_div">
<label >Email :</label>
<input type="text" name="email" value=" <?php echo $email ?> ">
<div class="error_msg"><?php echo $error['email']; ?></div>
</div>
<div class="input_div" >
<label >Pizza Title :</label>
<input type="text" name="title" value=" <?php echo $title ?> " >
<div class="error_msg"><?php echo $error['title']; ?></div>
</div>
<div class="input_div" >
<label >Ingredients (comma seperated) :</label>
<input type="text" name="ingredients" value=" <?php echo $ingredients ?> ">
<div class="error_msg"><?php echo $error['ingredients']; ?></div>
</div>
<div class="input_div" >
<input type="submit" class="submitBtn" name="submit" value="Submit">
</div>
</form>
<?php include 'template/footer.php' ?>
</html>
Other then the issues with whitespace in your inputs you should also be aware of XSS when inserting the values back into the form (like using " would break the form) and also don't populate the errors till needed, this will allow you to easily continue and do the success step without needing to loop over the $errors array and it also allows you to hide the <div class="error_msg"></div> element and only show when there is an error.
Also your missing <head> and <body>, presuming they are in the includes, but doing it that way would make it rather difficult to add additional elements or scripts.
<?php
$email = $title = $ingredients = '';
$error = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// check for email
if (empty($_POST['email'])) {
$error['email'] = 'Email is empty';
} else {
$email = $_POST['email'];
}
// check for title
if (empty($_POST['title'])) {
$error['title'] = 'Title is empty';
} else {
$title = $_POST['title'];
}
// check for ingredients
if (empty($_POST['ingredients'])) {
$error['ingredients'] = 'Ingredients is empty';
} else {
$ingredients = $_POST['ingredients'];
}
if (empty($error)) {
// do some thing with $email, $title, $ingredients
die(header('Location: ./thank-you.php'));
}
}
function xss_safe($value) {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
?><!DOCTYPE html>
<html lang="en">
<?php include 'template/header.php' ?>
<form action="form.php" method="POST">
<div class="input_div">
<label>Email :</label>
<input type="text" name="email" value="<?= xss_safe($email) ?>"/>
<?= isset($error['email']) ? '<div class="error_msg">'.$error['email'].'</div>' : '' ?>
</div>
<div class="input_div">
<label>Pizza Title :</label>
<input type="text" name="title" value="<?= xss_safe($title) ?>"/>
<?= isset($error['title']) ? '<div class="error_msg">'.$error['title'].'</div>' : '' ?>
</div>
<div class="input_div">
<label>Ingredients (comma seperated) :</label>
<input type="text" name="ingredients" value="<?= xss_safe($ingredients) ?>"/>
<?= isset($error['ingredients']) ? '<div class="error_msg">'.$error['ingredients'].'</div>' : '' ?>
</div>
<div class="input_div">
<input type="submit" class="submitBtn" name="submit" value="Submit">
</div>
</form>
<?php include 'template/footer.php' ?>
</html>
Seeing as your error checking is merely for empty/missed input fields it's easier to just make the inputs required as per HTML5. Here's a simplified version using placeholders for information after the form has been submitted.
Warning: If you are going to be inserting this data into a MySQL table, you need to sanitize the inputs first!
<?php
$email = $title = $ingredients = "";
if (isset($_POST["submit"])) {
$email = $_POST["email"];
$title = $_POST["title"];
$ingredients = $_POST["ingredients"];
}
echo "
<form method='POST'>
<label>Email:</label>
<input type='email' name='email' placeholder='$email' required>
<label>Pizza Title:</label>
<input type='text' name='title' placeholder='$title' required>
<label>Ingredients (comma seperated):</label>
<input type='text' name='ingredients' placeholder='$ingredients' required>
<input type='submit' name='submit' value='Submit'>
</form>
";
?>

PHP Still give me old data after form submit [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have form for updating some data on my category table. After i post my new data via form, page refresh but it still show old data to me.
public function updateCategory($status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id)
{
$sql = "UPDATE categories SET status = ?, main_category = ? , title = ?, bg_color = ?, meta_title = ?, meta_description = ?, meta_keywords = ? WHERE id = ?";
$stmt = $this->conn->prepare($sql);
try {
$stmt->execute([$status, $main_category, $title, $bg_color, $meta_title, $meta_description, $meta_keywords, $id]);
return true;
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
}
Form post area
$category = new Categories();
$id = $_GET['id'];
$query = $category->getCategoryDetails($id);
$result = false;
if (isset($_POST['categorySave'])) {
$result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id);
}
?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
<?php if ($result) { ?>
<div class="alert alert-success" role="alert">
<?=UPDATESUCCESS?>
</div>
<?php } ?>
<label for="title">Başlık</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>">
...
Its update data without problem. As an example if i change title 'Title New', it updates on my database. But after form submit, it stay as the same. 'Title Old'
move update code above the get code.
$result = false;
if (isset($_POST['categorySave'])) {
$result = $category->updateCategory($_POST['status'],$_POST['main_category'],$_POST['title'],$_POST['bg_color'],$_POST['meta_title'],$_POST['meta_description'], $_POST['meta_keywords'], $query->id);
}
$category = new Categories();
$id = $_GET['id'];
$query = $category->getCategoryDetails($id);
?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
<?php if ($result) { ?>
<div class="alert alert-success" role="alert">
<?=UPDATESUCCESS?>
</div>
<?php } ?>
<label for="title">Başlık</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Başlık" value="<?=$query->title?>">
...

Issues with passing value from HTML 'form' to PHP 'variable' to be used in a 'function' for greeting visitos [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have some issues with passing value from HTML "form" to PHP variable to be used in a function for greeting visitors. Here is my code:
<form action="welcome.php" method="post">
Please enter your name: <input class="inputbox" type="text" name="name">
<input class="submit" type="submit" name="submit">
</form>
</html>
Here is my welcome.php code:
<?php
echo 'Thank you for entering your name!';
if (isset($_POST['submit'])) {
$inputString = $_POST['name'];}
$last_character = substr($inputString, -1 );
// echo $last_character;
if ($last_character == 'a' || $last_character == 'а'){
echo 'Здравствуйте Госпожа!'; //greeting for female visitor
}
else {
echo 'Здравствуйте Сэр!'; //greeting for male visitor
}
?>
Problem is with the scope of the variable, create the variable outside of the if statement.
<?php
echo 'Thank you for entering your name!';
$inputString ='';
if (isset($_POST['submit'])) {
$inputString = $_POST['name'];}
$last_character = substr($inputString, -1 );
// echo $last_character;
if ($last_character == 'a' || $last_character == 'а'){
echo 'Здравствуйте Госпожа!'; //greeting for female visitor
}
else {
echo 'Здравствуйте Сэр!'; //greeting for male visitor
}
?>*

Categories