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.
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put$_POST,$_GETor any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake.mysqliis significantly less verbose, making code easier to read and audit, and is not easily confused with the obsoletemysql_queryinterface. Before you get too invested in the procedural style it’s worth switching over. Example:$db = new mysqli(…)and$db->prepare("…”)The procedural interface is an artifact from the PHP 4 era whenmysqliAPI was introduced and should not be used in new code.mysqliso mistakes aren't easily ignored..inc.phpfile. Those are typically named.incto prevent direct execution, and furthermore, are never intended to be called directly.