isabella isabella - 1 month ago 8
MySQL Question

Mysqli - PHP to collect data from database

I am trying to collect information from a HTML form then transfer the data to my sql database. I'm use Zampp phpmyadmin.

When I click submit, the data entered goes away but the success pop up does not pop up. There is no error. It just doesn't get sent to the database. Directly after the third IF statement is the ELSE statement that is not working - transferring the data to the database - this is in the second code file. I have no idea why.

This code is the index page

<!DOCTYPE html>

<?php
include("functions/functions.php");

?>

<html>
<head>

<link rel="stylesheet" href="styles/style.css" media="all"/>
</head>


<body>

<div class = "container">

<div id="head_wrap">
<div id="header">
<!-- logo goes here -->
<img src="images/logo.png"/ style="float:left"/>
<form method="post" action="" id="form1">
<strong>Email:</strong>
<input type="email" name="email" placeholder="Email" required="required"/>
<strong>Password:</strong>
<input type="password" name="pass" placeholder="*****" required="required"/>
<!-- the submit can be a button -->
<button name="login">Login</button>
</form>
</div>
</div>
<!-- this is where the second form and picture comes in on
main page -->
<div id="content">
<!-- second picture-->
<div>
<img src="images/image.png" style="float:left; margin-left:-40px;"/>
</div>
<div id="form2">
<form action="" method="post">
<h2>Sign Up Here</h2>
<table>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="u_name" placeholder="Enter your
name" required="required"/>
</td>
</tr>

<tr>
<td align="right">Password:</td>
<td><input type="password" name="u_pass" placeholder="Enter your
password" required="required"/>
</td>
</tr>

<tr>
<td align="right">Email:</td>
<td><input type="email" name="u_email" placeholder="Enter your
email" required="required"/>
</td>
</tr>

<tr>
<!-- this is where they will pick their state -->
<td align="right">Country:</td>
<td>
<select name="u_country"required="required">
<option>Select a country</option>
<option>USA</option>
<option>Afghanistan</option>
<option>India</option>
</select>
</td>
</tr>

<tr>
<td align="right" required="required">Gender:</td>
<td>
<select name="u_gender">
<option>Select a Gender</option>
<option>Female</option>
<option>Male</option>
<option>Other</option>
</select>
</td>
</tr>

<tr>
<td align="right" required="required">Birthday:</td>
<td><input type="date" name="u_birthday"/>
</td>
</tr>

<tr>

<td colspan="6">
<button name="sign_up">Sign up</button>
</td>
</tr>

</table>
</form>
<?php InsertUser();?>
</div>
</div>
<!-- this is where the second form and picture ends on
main page -->



</body>
</html>


This code is the functions.php that is transferring the info to the database.

<?php

$con = mysqli_connect("localhost","root","","social_network")
or die("Connection was not established");

function InsertUser(){
global $con;

if(isset($_POST['sign_up'])){
$name = $_POST['u_name'];
$pass = $_POST['u_pass'];
$email = $_POST['u_email'];
$country = $_POST['u_country'];
$gender = $_POST['u_gender'];
$b_day = $_POST['u_birthday'];
$date = date("d-m-y");
$status = "unverified";
$posts = "No";

$get_email = "select * from users where user_email='$email'";
$run_email = mysqli_query($con,$get_email);
$check = mysqli_num_rows($run_email);

if($check==1){
echo "<script>alert('Email has already been registered. Please try another one.')</script>";
exit();
}
if(strlen($pass)<8){
echo "<script>alert('Password should be minimum 8 characters.')</script>";
exit();
}
//this does not work. need to figure out why

else {
$insert = "INSERT into users (user_name,user_pass,user_email,user_country,user_gender,user_b_day,user_image,register_date,last_login,status,posts) values ('$name','$pass','$email','$country','$gender','$b_day','default.jpg','$date','$date','$status','$posts')";
$run_insert = mysqli_query($con,$insert);

if($run_insert){
echo "<script>alert('Registration Successful!')</script>";
}

}


}



}

?>

Answer

Use mysqli_error to get a meaningful error message:

if($run_insert)
{
  echo "<script>alert('Registration Successful!')</script>";
}
else
{
  echo sprintf("Error %s executing %s", mysqli_error($con), $insert);
}
Comments