Adding a string to a database - php

I'm trying to add a string to my database, where I have two columns: "id" and "image". The "id" column is supposed to increment and the "image" column should get a string. This is my phpcode:
<?php
$servername = "somename";
$username = "someusername";
$password = "somepssword";
$dbname = "somedatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$image = $_POST["image"];
$sql = "INSERT INTO photos (image) VALUES ('$image')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
the html form:
the html form:
<body>
<form method="post" action="phpcode.php">
<input type="text" name="message" size="55">
<input type="submit"name="submit" value="Send">
</form>
</body>
</html>
I use this app to send a post to the server: https://www.getpostman.com/ yet for some reason it only increments a value id and doesn't receive anything for image like here:
enter image description here

<form method="post" action="phpcode.php">
<input type="text" name="message" size="55">
<input type="submit"name="submit" value="Send">
</form>
As suspected, your name attribute field is wrong as it does not correspond to what you are trying to post .
Change to
<form method="post" action="phpcode.php">
<input type="text" name="image" size="55">
<input type="submit"name="submit" value="Send">
</form>
When submitting forms, PHP reads from your "name" attribute on your form. That is what you are posting to your controller file.

Related

The isset($_POST()) is returning false in the form

I've been trying to insert data into a database from the data i collected from a form.I used $_POST(id) but it was giving an error saying undefined index.So i used isset function.It fixed undefined index issue but now the isset is returning false so it is always inserting the default value of variable $a(ie 'n').
Heres the html file:
<html>
<head></head>
<form action="test.php">
<input type='text' id='a'/>
<input type='submit' value='sub'/>
</form>
</html>
and heres test.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$a="n";
if (isset($_POST['a']))
$a=$_POST['a'];
$sql = "INSERT INTO test
VALUES ('$a')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
whats wrong?
Also add the method attribute to your form. By default it is get. As well as the name attribute to your input tag
<form action='test.php' method='post'>
<input type='text' name='a' id='a' />
<input type='submit' value='sub'/>
</form>
You need to supply your fields with names, as it stands, these are not valid. As others have stated, you'll also need to ensure a method is set. The following is an example
<form action="test.php" method="post">
<input type='text' id='a' name='input_a'>
<input type='submit' value='sub'/>
</form>
Then in your output of $_POST should show your incoming information.

writing in a mysql table using POST & php

I'm trying to register the e-mail entered in my form by users in my SQL table. but I'm not receiving any errors and the data are not saved either!
<?php
echo "I was here !!!";
if(!empty($_POST['mail']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mailing";
echo "I was here !!!";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'INSERT INTO contact VALUES ("","'.$_POST['mail'].'")';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
and my html code:
<div class="form">
<p>Rester notifié par toutes les nouveautés !</p>
<form method="post" action="index.php" class="mainform">
<div class="field"><input type="text" class="field" name="mail" /></div>
<div class="submit"><input class="submit" type="button" value="Envoyer" /></div>
</form>
</div>
can you tell me what's the problem ?
change your button type .because if you want to submit the data by form then button type should be submit like that
<input class="submit" type="submit" value="Envoyer" />
Check if there's a value for $_POST['mail']. Your condition didn't handle empty value for $_POST['mail']. If there is a value. Change your query
INSERT INTO contact(email) VALUES ("'.$_POST['mail'].'")
Try this. Since you only need to add an email. Hope it helps.

Save input into database

Hello guys i need some help.I connected to database from server and can insert some info like $sql = "INSERT INTO Posts (Text_Post) VALUES ('Sample Text')";. Now I want to save on click text from <input type="text" /> to database. Can you tell me what i am doing wrong.
<?php
$servername = "google.com";
$username = "google";
$password = "google";
$dbname = "google";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])) {
$sql = "INSERT INTO Posts (Text_Post) VALUES ('".$_POST['text']."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>anonim</title>
</head>
<body>
<form name="form" action="" method="post">
<input type="text" name="text" id="text" value="Salut" /=>
<input type="submit" id="Submit" />
</form>
</body>
</html>
You're missing the name tag on your submit. When data is POST'ed to the server, it uses the name tag.
<input type="submit" id="submit" name="Submit">
Remember to watch your Capitals also - (since you're checking if Submit is SET then you need to POST the submit).
You could just do:
if(isset($_POST['text'])) {
Also, going off the comments: I'd suggest taking a look at this link because you're prone to SQL Injections.
when we are going to post a form using POST or GET. we should always give name to all our fieds so we get get them just using $_POST['name'] or $_GET['name']. In Your case just give a name to your submit tag and check whether data is submitted or not.
replace
<input type="submit" id="Submit" />
with
<input type="submit" id="submit" name="submit">
and check it like
if(isset($_POST['submit'])) {// it will only check where form is posted or not
// your code...
}

PHP and html in the same html page

I have a php file "send.php":
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "RegisterDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$n=$_POST["name"];
$a=$_POST["age"];
$g=$_POST["gender"];
$gr=$_POST["graduate"];
$ad=$_POST["address"];
if($_SERVER["REQUEST_METHOD"]=="POST"){
$sql = "INSERT INTO RegTbl (name, age, gender,graduate,address)VALUES ('".$n."','".$a."','".$g."','".$gr."','".$ad."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
and a html file form.html
<html>
<body>
<form method="post" action="send.php">
Name <input type="text" name="name"/><br><br>
Age <input type="text" name="age"/><br><br>
Gender <input type="checkbox" name="gender" value="male"/> M <input type="checkbox" name="gender" value="female"/> F<br><br>
Graduate <input type="radio" name="graduate" value="yes"/> YES <input type="radio" name="graduate" value="no"/> NO<br><br>
Address <textarea name="address"></textarea><br><br>
<input type="submit" name="submit_button" value="Send"/>
</form>
</body>
</html>
And i am trying to embed both codes in a single html page:
<?php
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "RegisterDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$n=$_POST["name"];
$a=$_POST["age"];
$g=$_POST["gender"];
$gr=$_POST["graduate"];
$ad=$_POST["address"];
if($_SERVER["REQUEST_METHOD"]=="POST"){
$sql = "INSERT INTO RegTbl (name, age, gender,graduate,address)VALUES ('".$n."','".$a."','".$g."','".$gr."','".$ad."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name <input type="text" name="name"/><br><br>
Age <input type="text" name="age"/><br><br>
Gender <input type="checkbox" name="gender" value="male"/> M <input type="checkbox" name="gender" value="female"/> F<br><br>
Graduate <input type="radio" name="graduate" value="yes"/> YES <input type="radio" name="graduate" value="no"/> NO<br><br>
Address <textarea name="address"></textarea><br><br>
<input type="submit" name="submit_button" value="Send"/>
</form>
</body>
</html>
But this is not working, can anybody help me to find the error? and i save this third file as test.html
You cannot execute php in an html file. Save the file with the .php extension.
If you put your php code in html and save the file as .html while running the html file the action will be required on a database which can be done only through php as php is server side and html is client side. So you need a server to run that which in your case is the localhost so you just save the file as .php and run it on the server so that your page can diretly interact with database.
Save it as test.php and run it. Because in html file php doesnt work

Update database data with submit button

I want to update a database so that when you put your text in a text box and click the submit button, the data will be sent to the database with a specific id. It is clear what I want to do in the code below. When I write something like this and run it, I receive a 403 error: Access forbidden. How can I fix this?
<?php
function updater($value,$id){
// Create a connection
$conn = new mysqli( 'localhost' , 'user_name' , '' , 'data_base_name' );
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE table_name SET name=$value WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
//$conn->close();
}
?>
<!DOCTYPE html>
<html>
<header>
</header>
<body>
<form action="<?php updater($_POST['name'],1); ?>" method="post" style="height:50px;width:50px;">
<input type="text" name="name" /><br><br>
<input type="submit" /><br/>
</form>
</body>
</html>
You need to put the URL inside the action attribute that does the form processing, not the function:
action="<?php updater($_POST['name'],1); ?>" // not this
action="" // empty for the same page
Also, usually the edited value fills the input and the record's id is added to the form in a hidden field. If processing is on the same page, best to leave the action empty. So a basic form could be like this:
<form action="" method="post">
<input type="text" name="name" value="<?=htmlspecialchars($row['name']) ?>"/><br>
<input type="hidden" name="id" value="<?=htmlspecialchars($row['id']) ?>"/>
<input type="submit" /><br/>
</form>
Above the form, the processing has to be added
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$conn = new mysqli( 'localhost' , 'user_name' , '' , 'data_base_name' );
updater($conn, $_POST['name'], $_POST['id']);
}
Besides, you must use safer prepared queries:
function updater($mysqli, $value, $id) {
$sql = "UPDATE table_name SET name = ? WHERE id= ?";
$update = $mysqli->prepare($sql);
$update->bind_param('si', $value, $id);
$update->execute();
return $update->affected_rows;
}
like this:
<?php
function updater($value,$id){
// Create connection
$conn = new mysqli( 'localhost' , 'user_name' , 'pass' ,'data_base_name' );
$value =mysqli_real_escape_string($conn,$value);
$id =mysqli_real_escape_string($conn,$id);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE table_name SET name='{$value}' WHERE id='{$id}'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
if(isset($_POST['name'])){
updater($_POST['name'],$_POST['id'])
}
?>
<!DOCTYPE html>
<html>
<header>
</header>
<body>
<form action="" method="post" style="height:50px;width:50px;">
<input type="hidden" name="id" value="1" />
<input type="text" name="name" /><br><br>
<input type="submit" /><br/>
</form>
</body>
</html>

Categories