I am having trouble inserting data into the database 'justrated'. Once the user has entered their business name it should create a new entry in the table 'businesses'. For some reason I cannot get it so that the data is entered in the table. Any advice is gladly appreciated.
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text" name="BusinessName" method="POST">
<input type="Submit" value="submit" name="submit" method="POST">
</form>
<?php
if (isset($_POST["submit"])){
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('".$_POST['BusinessName']."' )";
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
One of your problems is that $_POST['BusinessName'] is empty because the form was submitted using a GET request, not a POST request. The method=POST attribute goes on the <form> element. Eg:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit">
</form>
Also, you should escape the data properly before you insert it into the database:
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('" . $conn->real_escape_string ($_POST['BusinessName']) . "' )";
Furthermore, in these two lines:
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
you try to execute the same query twice using both the MySQL and MySQLi extension. You should remove the first line.
HTML Code
<form method="post" action="test1.php">
<input type="text" name="BusinessName" >
<input type="Submit" value="submit" name="submit" >
</form>
PHP Code
if (isset($_POST["submit"]))
{
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
Don't mix the mysql & mysqli....
Html:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit" >
</form>
Php:
Use
$conn->query($sql); not mysql_query()
hello please check this one i hope this working for you
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Related
I want to go from a HTML form to a SQL database using PHP.
Here is my current code:
PHP:
if(isset($_POST['submitIpG']))
{
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ($_POST['submitIpG'])";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
echo "<br />\n";
}
As you can see it's hard coded with a specific IP. However, I want it to use an IP from the user input in my form.
HTML:
<form method="post">
<input type="value" name="submitIpG" value=""/>
<input type="submit" name="submitIpG" value="ADD"/>
</form>
How do I do this? I've tried things such as:
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ('$_POST['submitIpB']')";
Without success.
Thanks!
You should set the action php script in form html also your inputs should be like
<form method="post" action="your_php_file.php">
<input type="text" name="submitIpG" placeholder="the ip input"/>
<input type="submit" name="submit" value="ADD"/>
</form>
and your_php_file.php should be like
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ('$_POST['submitIpG']')";
But the most important part is DO NOT USE sql queries like that. Please consider using prepared statement.
There is a good and simple example at w3school. this is the link you may want. https://www.w3schools.com/php/php_mysql_prepared_statements.asp
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.
My code is already inserting a data on the database, but only the Primary key(AUTO_INCREMENT) is the only adding. I can't get the date and the text.
Is there something wrong in my code?
Here is my code below:
HTML:
<form action="insertleave.php" method="post">
<label>Date Filed:</label>
<input type="date" name="datefiled">
<label>Date of Leave:</label>
<input type="date" name="leavedate">
</div>
<div class="medium-6 columns">
<label>Reason of Leave:</label>
<textarea rows="8" form="leaveform" name="reason"></textarea>
</div>
<input type="submit" class="expanded button" name="formSubmit" value="File Leave">
</form>
PHP:
<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['leavereason'];
$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
VALUES
('$ID','$EmployeeID','$DateFiled','$LeaveDate','$Reason')";
if (mysqli_query($conn, $sql)) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
In your text area, you given it a name "reason"
in your post variable, your value is "leavereason"
change the $leavereason = $_POST['leavereason']; to $leavereason = $_POST['reason'];
In you Reason of leave text area name of input is different.
Your variable name are different in your sql query and you are assigning to different variable.
Also your EmployeeID is empty here. there is no input for EmployeeID from html file or you should post it to php file.
Change you php code like this.
<?php
$datefiled = $_POST['datefiled'];
$leavedate = $_POST['leavedate'];
$leavereason = $_POST['reason'];
$config = parse_ini_file("phpconfig.ini");
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['dbname']);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO leaves (ID, EmployeeID,DateFiled, LeaveDate, Reason)
VALUES
('$ID','$EmployeeID','$datefiled','$leavedate','$leavereason')";
if (mysqli_query($conn, $sql)) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
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>
I have used an isset() function on 'submit' to store and retrieve my html form inputs. However all my SQL data in my table only gets displayed after I clicked the submit function on the browser as I programmed it that way.
I would like to make it now so that even when I refresh the browser the html form input remains permanently and I do not have to click submit to fetch the entire table and display (it rather displays itself straight away).
Please show me how I can go about this?
below is my code:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$question = $_POST['question'];
$description = $_POST['description'];
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
}
if(isset($_POST['question']) && $_POST['description']) {
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
</body>
</html>
If I've correctly understood the question, you would like to always display all entries and add new data only when it's required. For this goal, you can connect and retrieve data independently from form submission, and insert new data only when required.
<?php
require_once "connection.php";
// Connect to database
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
}
// Insert new data if required
if(isset($_POST['submit']) && isset($_POST['question']) && isset($_POST['description']))
{
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if(!$conn->query($sql)) {
echo "error during insert: " . $conn->error;
exit();
}
}
// Display data
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
// Close database connection
$conn->close();
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
</body>
</html>
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$question = $_POST['question'];
$description = $_POST['description'];
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
}
if(isset($_POST['question']) && $_POST['description']) {
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
}
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
As I told you in the other question..