HTML SQL server connection using PHP - php

I have a HTML form and I want to use the results to filter the data available in a SQL file. I'm trying to connect to a local MySQL server but It doesn't work, i don't achieve to enter in the table() function.
I use the following code:
<form id="form_id" action="food_values.php" method="post" name="myform">
...
<input onclick="table" type="button" value="Submit">
</form>
The php file contains the following code:
<html>
<body>
<?php
function table(){
echo ("INSIDE");
$con = mysql_connect("localhost","root"," ");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "CONNECTED";
}
}
?>
</body>
</html>
It doesn't show anything... Do you know where is the error?

You can not execute a PHP-Function with "onclick".
Execute PHP function with onClick
With a form, you load the whole .php-file if you click on the submit.
The following code should work for you:
<html>
<body>
<?php
echo ("INSIDE");
$con = mysql_connect("localhost","root"," ");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "CONNECTED";
}
?>
</body>
</html>
And the HTML
<form id="form_id" action="food_values.php" method="post" name="myform">
...
<input type="submit" value="Submit">
</form>

Change
<input onclick="table" type="button" value="Submit">
into
<input type="submit" name="submit" value="Submit">
As the action in form takes care of where to direct the data in php.
and in 'food_values.php' page you dont have to define function as its not javascript.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
This will connect the database.
And for validation of the form follow the SQL commands. For futher detail visit
https://www.w3schools.com/php/php_form_validation.asp

Related

My SQL workbench database is not updating when html form is submitted

I made a HTML form to link it to mysql workbench. The form or the php connection codes are not showing any errors but the changes arent reflected in the database. I have deleted some of the code which isn't important since the site doesn't allow me.
HTML FORM CODE
<form action="qrconnect.php" method="post">
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="Name">
</div>
.
.
.
<div class="button">
<button name="submit" type="submit">Submit</button>
</div>
</form>
PHP code
<?php
include_once("connect.php");
include_once("qr.html");
/*Data from the html form is on the right. The objects that will be composed of that data is on the left.*/
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$EMail = $_POST['E-mail'];
$Phone= $_POST['Phone'];
$Designation =$_POST['Designation'];
$Dept= $_POST['Dept'];
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
mysqli_query($mysqli, "INSERT INTO schema1.emp_table(Name,E-mail,Phone,Designation,Dept)
VALUES('$Name','$EMail','$Phone','$Designation','$Dept')");
echo"<font color='green'>Data added successfully";
}
?>
PHP Connect code
<?php
$host="localhost";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="schema1";
$mysqli = mysqli_connect($host, $user, $password, $dbname, $port,
$socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
//$con->close();
?>

Downloads PHP instead of Running

I am trying to make a form that submits data to my database on mySQL workbench. When ever I click submit it ends up downloading the php file instead of running. All files are in the same folder.
inventorylog.html
<form action="insert.php" method="post">
<input required type="text" name="sku_input" placeholder="Sku #" />
<br>
<input value="Add Sku" type="submit" name="submit" />
</form>
connect.php
<?php
$host="localhost";
$port=3306;
$socket="/tmp/mysql.sock";
$user="root";
$password="";
$dbname="Logs";
$con = mysqli_connect($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
//$con->close();
?>
insert.php
<?php
/*Needs the connection object created in the connect.php file to work*/
header("LOCATION:inventorylog.html");
require('connect.php');
/*require('inventorylog.html');*/
/*Data from the html form is on the right. The objects that will be composed of that data is on the left.*/
if(isset($_POST['submit'])) {
$skunum = mysqli_real_escape_string($con, $_POST['sku_input']);
echo 'sku_input';
$sql = "INSERT INTO skulist (sku_input)
VALUES ('$skunum')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
}
?>

Pre Complete HTML Form with PHP and MYSQL

I am creating a simple page which updates a single record tempKey=1, single field reqdTemp MySQL dBase. I have the form working fine; it updates the record, then returns to the initial form ready for the user to change the temperature again.
Q: I would like the form to be pre-populated with the existing information from the database so the user sees the current required temperature about to be changed. I'm not sure where to start!!
The form, updateTemperature.php, is this:
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>
The post script, insert.php is this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$sql = "UPDATE PiBQ_Temp SET reqdTemp = '$_POST[setTemp]' WHERE tempKey = 1";
mysqli_query($con,$sql);
echo "1 record added";
header ('location: PiBQ_Temp2.php');
mysql_close($con)
?>
To pre-populate the form, query the database for the current value and set that in the returned HTML. So your updateTemperature.php could become something like this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$currentTemp = 100; // some default
$sql = "SELECT reqdTemp FROM PiBQ_Temp WHERE tempKey = 1";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$currentTemp = $row['reqdTemp'];
}
mysql_close($con);
?>
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" value="<?= $currentTemp ?>" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>

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...
}

Input data does not get stored in database, but no error

I need to get a user's comment and store in the database table column 1 and display the entered comment in different table. The code works fine with no errors, but the comment does not get stored in the database.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if (isset($_POST['Submit'])) $Comment = isset($_POST['Comment']) ? $_POST['Comment'] : '';
$sql = "INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res = $conn->query($sql);
if ($res) {
echo "Successful";
echo "<br />";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
</body>
</html>
try this.Notice the opening and closing bracket.
<head>
<title></title>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "escalation";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}else {
echo "ERROR";
}
}
?>
</body>
</html>
Another info and a sort of advice though it does not concern to your question is please use prepared statement that will help prevent sql injection.
You can read php manual about mysqli prepared statement here .
You might also want to check PDO prepared statement click here.
You might also want to check this this full helper class for your crud operation that i personally created.That also uses PDO prepared statement.
Hope that helps somebody.
Try this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server="localhost";
$username="root";
$password="";
$database="escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
}
?>
</body>
</html>
Did you check if the $_POST is giving the output you want. If yes ,then try the mysql command on the commandline and see if it shows any error ... Your command should work, i tried it in my commandline, it worked.
Either you havent set the correct data types and lengths of values in the database table or try this:
A safe command which always works is
"INSERT INTO `css`(`Dis_Cmt`)VALUES('.$Comment.')"
First thing I notice:
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
should be
(!$conn)
Put a echo before the INSERT. If its show up, the problem maybe the
query or the table field;
Insert any value in css table using the
phpmyadmin for testing;
Use prepared statement to avoid sql
injection. Below, a example.
$Comment = $_POST['Comment'];
$sql = "INSERT INTO css (Dis_Cmt) VALUES (?)";
$statement = $conn->prepare($sql);
$statement->bind_param('s', $Comment);
if($statement){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
$statement->close();
Good luck!

Categories