Before anyone says this is a duplicate, I have checked and tried the solutions from this previously asked question. My question is different, I believe, because I don't have a separate php file - it's coded with tags in my HTML (so everything is in the same document).
Here is my PHP (database info left empty):
<?php
session_start();
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$dbname = '****';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$email=$_POST['email'];
$query="INSERT INTO tableName(email)
VALUES('$email')";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is my Materialize/HTML form:
<form action="/thankyou.php" method="POST">
<p class="input-header">Enter Your Email:</p>
<input id="email" type="email" name= "email" class="validate" required>
<br></br>
<input class="waves-light btn indigo lighten-2" type="submit">
<br></br>
<br></br>
<br></br>
</form>
Any ideas for why it's not working? I checked my MAMP phpmyadmin database and nothing is getting added. Please let me know if you have any suggestions! Thanks!
This will help you: As you haven't added mysqli_query and because of that it wasn't adding data. Also here I am checking whether the form is submitted as you mentioned it's a single file.
<?php
// check if form is submitted
if(!empty($_POST['email'])) {
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$dbname = '****';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// this line prevents sql injection
$email = mysqli_real_escape_string($connection, $_POST['email']);
$query="INSERT INTO tableName(email) VALUES('$email')";
// this statement runs your query and actually adds data to mysql
if (mysqli_query($connection, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
mysqli_close($connection);
}
?>
Related
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'm still learning code. I'm in college and have entered everything like my professor to the tee. His code is running through to his DB, but mine is throwing a PHP 500 error.
I don't think that it is a code error, but I was hoping another set of eyes or someone with more experience code see an issue I'm not able to spot. I have tried changes to some of my variables and both the form and the PHP connection commands.
<head>
<title>Post Your Content</title>
<link rel="stylesheet" href="stylesheet.css" />
</head>
<body>
<div class="header">
<h1>Create Content for the Database!</h1>
<p><i>If only the library of Alexandria was backed up to the cloud...</i></p>
</div>
<div class="topnav">
Home Page
Create Content
Link
Link
</div>
<form name="postcontent" method="post" action="InserttoDB.php">
<p>Author:
<input type="text" name="Author" id="Author"></p>
<p>Title:
<input type="text" name="Title" id="Title"></p>
<p>Topics:
<input type="text" name="Tags" id="Tags"></p>
<p>Content:
<input type="text" name="Content" id="Content"></p>
<input type="submit">
</form>
<div class="footer">
<h2>I made this for school!</h2>
</div>
</body>
<?php
//Catch information
//Render data
//Connect to database
//Insert data into database
$Author = htmlspecialchars($_POST['Author']); //grab value
echo "DEBUG - Author is" . $Author;
$Title = htmlspecialchars($_POST['Title']); //grab value
echo "DEBUG - Title is" . $Title;
$Tags = htmlspecialchars($_POST['Tags']); //grab value
echo "DEBUG - Tags are" . $Tags;
$Content = htmlspecialchars($_POST['Content']); //grab value
echo "DEBUG - Content is" . $Content;
//Database
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "cms system";
$mysqli = new mysqli(
$db_host,
$db_user,
$db_password,
$db_db,
);
//create connection
$connection = new mysqli ($servername, $username, $password, $dbname);
//check connection
if($connection->connect_error) {
die("Connection Failed: " . $connection->connect_error)
}
//INSERT INTO `content` (`PostID`, `Author:`, `Title:`, `Tags:`, `Content:`) VALUES ('test', 'test', 'test', 'test');
$sql = "INSERT INTO content(Author, Title, Tags, Content) VALUES ('$Author', '$Title', '$Tags', '$Content')";
//Run SQL
if(mysqli_query($connection, $sql)) {
echo "New Post Created!";
}
//Error MSG
else {
echo "Error: " . $sql . "" . mysqli_error($connection);
}
//Close DB Connection
$connection->close();
?>
This is a screen shot of my database and the table for more information. Php is 7.4.1 and I am using MAMP. I am using visual studio to write. The browser is chrome.
php database screenshot
Change that professor as soon as you can.
Change your db connection settings:
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "cms system";
$mysqli = new mysqli(
$db_host,
$db_user,
$db_password,
$db_db,
);
//create connection
$connection = new mysqli ($servername, $username, $password, $dbname);
To :
$servername = '127.0.0.1';
$port = 3306; //Your port can be 3306 or 3307 or 3308
$username = 'root';
$password = 'root';
$dbname = 'test'; //no space between db name like yours 'cms system'
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$connection = new mysqli($servername , $username , $password , $dbname , $port);
$connection ->set_charset($charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
unset($servername , $dbname , $username , $password , $charset);
Check your database name shouldnt have spaces in between. 'cmssystem'
I created this for one of my projects. We have a webshop where users can enter their credentials and order products. The current solution puts all the data into a .csv-file and I was tasked with creating a mysql database as a new solution.
I added a simple HTML insert for the user to enter his name, but if I try to run the script I get a syntax error for line 19. I'm new to programming and therefore not sure what the error is here.
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "localhost";
$password = "";
$dbname = "test"
// create a variable
$Vorname=$_POST['Vorname'];
$Nachname=$_POST['Nachname'];
//Execute the query
mysqli_query($connect "INSERT INTO tbl_bestellungen(Vorname,Nachname)
VALUES('$Vorname','$Nachname')");
<?php include 'database.php';>
if(mysqli_affected_rows($connect) > 0){
echo "<p>Bestellung erfasst</p>";
} else {
echo "Bestellvorgang fehlgeschlagen<br />";
echo mysqli_error ($connect);
<h2>Text Input</h2>
<form>
Vorname:<br>
<input type="text" name="Vorname">
<br>
Nachname:<br>
<input type="text" name="Nachname">
<input type="submit" name="button1" value="Senden">
</form>
</body>
</html>
Thanks in advance.
Well you should do like this way:
$servername = "localhost";
$username = "localhost";
$password = "";
$dbname = "test"
$dbConn = mysqli_connect($servername, $username, $password, $dbname);
if(!$dbConn){
echo "No Db connected";
}
//above connection code should be in a separate file and include in all files or header
$Vorname=$_POST['Vorname'];
$Nachname=$_POST['Nachname'];
$query = "INSERT INTO tbl_bestellungen (Vorname,Nachname)
VALUES('$Vorname','$Nachname')";
or you can set query like
$query = "INSERT INTO tbl_bestellungen (Vorname,Nachname)
VALUES('".$Vorname."','".$Nachname."')";
if($dbConn->query($query)){
echo "Record inserted !";
}else{
echo "Record cannot be inserted !";
}
I am coding my first php app and I am basing it off a tutorial I was working on that worked. My code as of right now works fine until I get to the $var = $connection->query("INSERT INTO . . . etc.
At this point, the code immediately after the first $ just shows up as plaintext in firefox. (google shows the whole thing as text blah).
I will post my code here:
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cowboyserver";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword);
mysqli_select_db($dbName, $conn);
$email = ($_POST['email']);
if(!$conn){
echo 'error';
}else{
$query = $conn->query("INSERT INTO email_list (email) VALUES ('$email')");
}
mysqli_query($query);
header("Location: ../index.html?signup=success");
echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>' ;
Additionally, here is the HTML : : : :
<form autocomplete="on" action="includes/signup.inc.php" method="POST">
<input type="email" name="email" placeholder="put your email here" class="blah"/>
</form>
EDIT:
After trying some solutions, I have found that my php code breaks at a seemingly random point in the code. In the second answer posted, for example, the php code runs until it gets to "$conn->connect_error" in the if statement and then prints out everything after the -> instead of executing it.
Changes you needed:-
1.Need to change file name from signup.inc.php to signup.php and then use it in from like below:-
<form autocomplete="on" action="includes/signup.php" method="POST">
<input type="email" name="email" placeholder="put your email here" class="blah"/>
</form>
2.change in signup.php(the file you renamed) code (changes are commented):-
<?php
//comment these two lines when code executed successfully
error_reporting(E_ALL);
ini_set('display_errors',1);
if(!empty($_POST['email']){ // check posted data coming or not
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cowboyserver";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword,$dbName); //add dbname here itself
//check conneced or not
if(!$conn){ // $ missed
die('connection problem'.mysqli_connect_error());//check for real connection problem
}else{
$email = $_POST['email'];// remove ()
//don't mix oop way to procedural way and use prepared statements
$stmt = mysqli_prepare($conn, "INSERT INTO email_list (email) VALUES (?)"));
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $email);
/* execute query */
if(mysqli_stmt_execute($stmt)){//check query executes or not
header("Location: ../index.html?signup=success");
echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>';
exit();
}else{
echo "insersion failde because of".mysqli_error($conn);
}
}
}else{
echo "please fill the form";
}
Note:- always use prepared statements to prevent from SQL INJECTION.Thanks
Try this. Change your form to include a submit button. Then only you can access values using $_POST.
<form autocomplete="on" action="includes/signup.php" method="POST">
<input type="email" name="email" placeholder="put your email here" class="blah"/>
<input type="submit" value="Form Submission" name="submitBtn">
</form>
Your signup.php page:
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cowboyserver";
// Create connection
$conn = new mysqli($conn = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName));
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitBtn'])) { //form submission occured
$email = $_POST['email'];
$sql = "INSERT INTO email_list (email) VALUES ('$email')";
if ($conn->query($sql)) {
echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>';
header("Location: ../index.html?signup=success");
exit();
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Form Submission Error";
}
$conn->close();
?>
Hope it's helpful.
I am trying trying to receive data by making a html form and then adding receiving data by $_GET method.Whenever I try to submit my data I get an error saying 'Your file was not found' in my chrome browser,so I tried opening my php page in chrome by typing "localhost/...." in my address bar and there it was displaying 'Database NOT Found'
here is my php code:-
<html>
<?php
$user_name = "root";
$password = "";
$database = "mysql"; //mysql is name of my database
$server = "localhost";
$db_handle = mysql_connect($server , $user_name, $password,"addresbook"); //adressbook is where all the tabels are
$db_found = mysql_select_db($database, $db_handle);
if (!$db_found) {
print "Database Found ";
$x=$_GET['fname'];
$y=$_GET['sname'];
$z=$_GET['address'];
$sql="INSERT INTO addresbook(First_Name,Surname,Address) VALUES('".$x."','".$y."','".$z."')";
mysql_query($sql,$db_handle);
}
else {
print "Database NOT Found ";
}
?>
</html>
here is mt html code:-
<form action="practic.php"method="get">
Firstname:<input type="text" name="fname"><br>
Lastname:<input type="text" name="sname"><br>
Address:<input type="text" name="address"><br>
<input type="submit">
</form>
btw i am using wamp server.Thanks in advance.
$db_found will be true on success, so your condition should be
if ($db_found) { // make DB changes etc.
and switch to MySQLi or PDO and use prepared statements as already mentioned, refer to the manual:
http://php.net/manual/en/mysqli.prepare.php
use mysqli_connect because mysql_connect is now deprecated.
$db_handle = mysqli_connect($server , $user_name, $password,$database);
refer here for connection
http://www.w3schools.com/php/func_mysqli_connect.asp
Hi change your code to use MysqLi or use PDO
<?php
$user_name = "root";
$password = "";
$database = "mysql"; //mysql is name of my database
$server = "localhost";
$con = mysqli_connect($server,$user_name,$password,'adresbook');//adresbook is where all the tabels are
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Change database to "adresbook"
$db_found = mysqli_select_db($con,'adresbook');
if ($db_found) {
print "Database Found ";
$x=$_GET['fname'];
$y=$_GET['sname'];
$z=$_GET['address'];
$sql="INSERT INTO addresbook(First_Name,Surname,Address) VALUES('".$x."','".$y."','".$z."')";
mysqli_query($con, $sql);
}
else {
print "Database NOT Found ";
}
?>
Your phpMyadmin should look like this with the database addresbook with a single s.
I have a Drupal-7 local website and I have a form/button in an article. I want when the button is clicked to be able and track the username and the password of those who clicked the button. The form html code is the following:
<form action="mysql-query.php" method="post" onsubmit="return Press(this)">
<input type="text" name="email" style="display:none;">
<input type="submit" value="Press here" id="test">
</form>
I have created from phpmyadmin a new column on users table called button, which is an Int(1) with default value=0 and the mysql-query.php file contains this code:
<?php
$_POST["email"];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "drupal";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
mysql_select_db($dbname);
$sql = mysql_query("UPDATE users SET button='1' WHERE email=loggedInMail");
$output = mysql_query("SELECT name, mail FROM users WHERE button='1'");
?>
I clicked on the button, and checked users table, but all the users I created have a button 0 value. Any ideas of what's wrong?
change line:
$_POST["email"];
to
$postemail = $_POST["email"];
change line:
$sql = mysql_query("UPDATE users SET button='1' WHERE email=loggedInMail");
to
$sql = mysql_query("UPDATE users SET button='1' WHERE email='".$postemail."'");