Form not posting what i need it to - php

I am experimenting with php,
How ever i can't find out how to post input data from one file to an other one.
This is the current code
<?php
if (isset($_POST['mysql'])) {
$my_file = 'test/core/config.php';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = '<?php
//connect to db
$host = "$host"; //i want the form value to be imported here
$user = "$user";
$port = "$port";
$pass = "$pass";
$db = "$db";
$sql = conn($host, $user, $port, $pass, $db);
?>';
fwrite($handle, $data);
}
?>
Here is the html form
<form class="form" action="testinstall.php" method="post">
<label for="host">Host</label>
<input type="text" name="host">
<label for="user">Username</label>
<input type="text" name="user">
<label for="port">Port</label>
<input type="text" name="port">
<label for="pass">Password</label>
<input type="text" name="pass">
<label for="db">Databace</label>
<input type="text" name="db">
<button type="submit" name="mysql">Submit</button>
</form>

Try in this away:
$data = "<?php
//connect to db
$host = '$host';
$user = '$user';
$port = '$port';
$pass = '$pass';
$db = '$db';
$sql = conn($host, $user, $port, $pass, $db);
?>";
You should use " outer to print values of variables.

Related

Error in saving data from HTML form to MySQL database

In this I created a simple HTML form in which the email and password entered from HTML form are not saving in the MYSQL database! what changes do I made so that data can be save in MYSQL database.
This is my HTML code :
<form style="position: relative;" action="pass.php" method="post" >
<div style="position: absolute; left: 107px; top: 130px; text-align: center; width: 450px;">
<input type="email" name="email" style="border:none" size="45" placeholder="Email" /><br>
<br>
<input type="password" name="password" style="border:none" size="45" placeholder="Password" /><br /><br>
<button type="submit" class="inbutton"></button>
</div>
</form>
This is php code :
<?php
if( $_POST )
{
$con = mysql_connect("mysqlhostname","username","password","databasename");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$users_email = mysql_real_escape_string($users_email);
$users_password = mysql_real_escape_string($users_password);
$query = "INSERT INTO pass (`email`, `password`) VALUES ('$users_email', '$users_password');";
mysql_query($query);
mysql_close($con);
}
?>
In Html file : give the name property of submit button
<html>
<body>
<form style="position: relative;" action="pass.php" method="post" >
<div style="position: absolute; left: 107px; top: 130px; text-align: center; width: 450px;">
<input type="email" name="email" style="border:none" size="45" placeholder="Email" /><br>
<br>
<input type="password" name="password" style="border:none" size="45" placeholder="Password" /><br /><br>
<button type="submit" class="inbutton" name="Save">Save</button>
</div>
</form>
</body>
</html>
This is php code :
In Your PHP File Do the small changes : pass.php
<?php
if( isset( $_POST['Save'] ) )
{
$con = mysql_connect("mysqlhostname","username","password","databasename");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( "databasename", $con );
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$users_email = mysql_real_escape_string($users_email);
$users_password = mysql_real_escape_string($users_password);
$query = "INSERT INTO pass ( email, password ) VALUES ( '$users_email', '$users_password' )";
mysql_query( $query, $con );
mysql_close($con);
}
?>
In an offline discussion, we discovered that OP's code is not going into the outer if statement. Here's some sample code on how I would structure it.
Things to notice:
give the submit button a name and then check for that in $_POST
Use PDO instead of mysql API
wrap everything in a try/catch block to see any errors
Gotta run. Good luck!
<form action="pass.php" method="post" >
<input type="email" name="email" size="45" placeholder="Email" />
<br>
<input type="password" name="password" size="45" placeholder="Password" />
<br>
<input type="submit" name="submit" value="Submit">
</form>
/*****************************************/
/**************** pass.php ***************/
/*****************************************/
<?php
// Please use PDO instead of mysql because mysql is deprecated and officially removed as of PHP 7.
// Read about it here: https://phpdelusions.net/pdo
if(isset($_POST['submit'])) {
// Wrap everything in a try/catch block so you can actually see the error.
try {
/*****************************************/
/************* DB CONNECTION *************/
/*****************************************/
// Change this to match your DB credentials
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// This is your DB connection. Use it below.
$pdo = new PDO($dsn, $user, $pass, $opt);
/*****************************************/
/************ FORM PROCESSING ************/
/*****************************************/
// Get values from form.
// Do any validation you want here.
$email = $_POST['email'];
$password = $_POST['password'];
/*****************************************/
/*************** DB INSERT ***************/
/*****************************************/
$stmt = $pdo->prepare('INSERT INTO pass (email, password) VALUES (?, ?)');
$stmt->execute(array(
$email,
$password
));
/*****************************************/
/*********** OUTPUT ANY ERRORS ***********/
/*****************************************/
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
php file if($_POST) It should be if( isset( $_POST ['Save'] ) ) and
<button type="submit" class="inbutton"> it should be <button type="submit" class="inbutton" name="Save">
try below php code
<?php
if( isset( $_POST['Save'] ) )
{
$servername = "mysqlhostname"; //localhost or your server name
$username = "username"; //root or your username
$password = "password"; //password or server hasn't password it should be `$password = "";`
$dbname = "databasename"; //your database name
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Could not connect: " . $con->connect_error);
}
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$sql = "INSERT INTO pass (email, password) VALUES ('$users_email', '$users_password')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$con->close();
}
?>
I got result

PHP/MYSQL Log-in update troubles

I have been working on this Login script for awhile and everything works, except this update function. I have tried changing variable name and everything else. On UpdateUser.php, the code works if I insert variables instead of the $[POST] variables. I am at a loss. Any help would be greatly appreciated. Sorry for the messy code, this is a class assignment, so I wasn't worried about password security at the moment.
This is index4.php
<form id="form" action="index4.php" method="post">
<h2>Update Your Login</h2>
UserName:<br>
<input type="text" id="useuserName" required />
<br>
Password:<br>
<input type="text" id="usepassWord" required />
<br>
First Name:<br>
<input type="text" id="usefirstName" required />
<br>
Last Name:<br>
<input type="text" id="uselastName" required />
<br>
<input id="updateuser" type ="submit" />
</form>
<script>
$('#updateuser').click(function() {
var useID = $_SESSION["id"];
var useuserName = $("#useuserName").val();
var usepassWord = $("#usepassWord").val();
var usefirstName = $("#usefirstName").val();
var uselastName = $("#uselastName").val();
var usePermissions = $_SESSION["Permissions"];
$.ajax({
type : 'POST',
url : '',
data :{action:'updateuser', useID:useID, useuserName:useuserName, uselastName:uselastName, usePermissions:usePermissions},
error: function (html) {
alert( "What the duck" );
},
});
});
</script>
This is the UpdateUser.php file
<?php
//Update
if($_POST['action'] == 'updateuser'){
//Set Variables
$servername = "localhost";
$username = "root";
$password = "";
$db = "userdb";
//Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection: Failed! " . $conn->connect_error);
}
//Actual Code
$useID = $_POST['useID'];
$useuserName = $_POST['useuserName'];
$usepassWord = $_POST['usePassword'];
$usefirstName = $_POST['usefirstName'];
$uselastName = $_POST['uselastName'];
$usePermissions = $_POST['usePermissions'];
//Create Query
$sql = "UPDATE users SET userName = '$useuserName', Pass = '$usepassWord', firstName = '$usefirstName', lastname = '$uselastName', Permissions = '$usePermissions' WHERE id =" . $useID ."";
//Did it work Check
if ($conn->query($sql) === TRUE) {
echo "Cool";
} else {
echo "What " . $conn->error;
}
//Close Out
$conn->close();
}
?>

Can't submit an input for later use. php

I am trying to use a search bar to grab the users input but when i input and submit nothing happens, I its not submitting properly but i can't for the life of me find my mistake. I have copied the input code from a previously created login page which works fine and therefore baffles me even more.
<div class="container">
<div class="form-group">
<?php
$stocksymbol = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["stocksymbol"])) {
$stocksymbolErr = "Please enter Username.";
} else {
$stocksymbol = test_input($_POST["stocksymbol"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}}
$servername = "localhost";
$username2 = "root";
$password2 = "";
$dbname = "mydb";
$mysqli = new mysqli($servername, $username2, $password2, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$query = "SELECT * FROM tblstocks WHERE Symbol = '$stocksymbol'";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$_SESSION['stockavailable'] = true;
$_SESSION['stock']= $row;
header('Location: item.php');
}
$result->free();
}
$mysqli->close();
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="stocksymbol">Search:</label>
<input type="text" class="form-control" id="stocksymbol">
<br><br>
<input type="submit" class="btn btn-default" name="submit" value="Submit">
</form>
</div>
The input element must have an attribute name with the value to be the key that you are expecting on the server i.e. stocksymbol to be able to receive whatever entered in the element.
<input type="text" class="form-control" id="stocksymbol" name="stocksymbol">

Trying to add items to a database using php

I am trying to a items to my database (sql) using php and a form, however the data is not being added and nothing seems to happening i just stay on the create.php page.
php code
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "PolyTest";
// Create connection
$conn = mysql_connect($servername, $username, $password);
mysql_select_db($dbname)
$doorName = $_POST['doorName'];
$doorDes = $_POST['doorDes'];
$doorPrice = $_POST('doorPrice');
$doorColour = $_POST('doorColour');
$doorImage = $_POST['doorImage'];
if(!$_POST['submit']){
echo "please fill in the boxs";
header('Location: dooradd.php');
} else {
mysql_query("INSERT INTO Doors ('ID', 'name', 'description', 'price', 'colour', 'image') VALUES(NULL, '$doorName', '$doorDes', '$doorPrice', '$doorColour', '$doorImage')") or die(mysql_error());
echo "Door been added!";
header('Location: doorlist.php');
}
?>
HTML FORM
<form class="add" action="doorCreate.php" method="post">
<input type="text" name="doorName" value="doorName">
<input type="text" name="doorDes" value="doorDes">
<input type="text" name="doorPrice" value="doorPrice">
<input type="text" name="doorColour" value="doorColour">
<input type="text" name="doorImage" value="doorImage">
<input type="submit" name="submit">
</form>
change mysql_select_db($dbname) with mysql_select_db($dbname);
and change;
$doorPrice = $_POST('doorPrice');
$doorColour = $_POST('doorColour');
with
$doorPrice = $_POST['doorPrice'];
$doorColour = $_POST['doorColour'];

I can't put post values from forms in variables?

In the code below the variables I create ($host, $username etc.) remain empty. What am I doing wrong? When I just make a variable with a regular string it works fine.
<form action="" method="post">
<input type="text" name="host" id="host-input" value="" />
<input type="text" name="dbname" id="db-input" value="" />
<input type="text" name="password" id="password-input" value="" />
<input type="text" name="username" id="username-input" value="" />
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit']))
{
$host = $POST['host'];
$username = $POST['username'];
$dbname = $POST['dbname'];
$password = $POST['password'];
$file = 'testbestandje.php';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= '<? $conn=mysql_connect("'.$host.'","'.$dbname.'","'.$password.'") or die("Kan geen verbinding maken met de DB server");
mysql_select_db("'.$username.'",$conn) or die("Kan database niet selecteren"); ?>';
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
$_POST is the correct variable name
$host = $_POST['host'];
$username = $_POST['username'];
$dbname = $_POST['dbname'];
$password = $_POST['password'];
Documentation

Categories