mobimobi mobimobi - 5 months ago 10
PHP Question

Unable to insert rows into MySQL database using PHP-MySQLi

EDIT: Question was marked as a duplicate for asking about backticks and other things? That was not my intention. I was wondering why this specific code was not executing correctly.

Also, I have added the

mysqli_query($mysql)
into my code, however it is still not executing. I'm checking if the problem lies in connecting to the DB now.

My goal is to create a register page in which
PHP
takes inputs and adds the data to a
MySQL
table through MySQLi.

The name of the table is userinfo. I have also checked config.php and verified that the password, username, and database are all correct.

$db
is in
config.php


The relevant PHP code goes as follows:

<?php
include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {

// username and password sent from form
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$ip = mysqli_real_escape_string($db, "N/A");

//checks for different ips (no masking allowed unless u tryhard)
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
//inserts info into tables
$sql = "INSERT INTO userinfo (email, username, password, ip) VALUES ($email, $username, $password, $ip);";

mysqli_query($mysql);
}
?>


All inputs on the table are text-based.

<form action = "" method = "post">
<label>Email: </label><input type = "text" name = "email" class = "box" /><br/><br />
<label>Create UserName: </label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Create Password: </label><input type = "password" name = "password" class = "box" /><br/><br />
<label>Verify password: </label><input type = "password" name = "vpassword" class = "box"/><br/><br/>
<input type = "submit" value = " Submit "/><br />
</form>

Answer

$sql = "INSERT INTO userinfo (email, username, password, ip) VALUES ('$email', '$username', '$password', '$ip')";