Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been here many times. I cannot get info to post to database. Can someone explain to me what im doing wrong so I can resolve the issue?
I've ran the code on a local host with E_All and E_strict and the only thing that comes up is undefined variables. Not the ones that are supposed to be posting but the username/pass/db name ones.
First is the Form I am using and the second is the php.
<form method="post" action="hg.php">
<div id="box">
<font size="1px">
Winner must be 18 years or older and have a valid email address. Drawings will be held 12/31/2013. If for any reason either winners do not claim prize within 30 days, that winner will be null and void and we will draw again on 01/31/14 for that winners prize choices. If no winner claims their prize within 30 days from 01/31/14, all drawings and winners will be
forfeit except for the already claimed prizes if any.
</font></div>
<div class="clear"> </div>
<div id="box">
<label for="textfield" style="margin-top:15px;">First Name</label>
<input id="textfield" type="text" name="first" />
</div>
<div id="box">
<label for="textfield" style="margin-top:15px;">Last Name</label>
<input id="textfield" type="text" name="last" />
</div>
<div class="clear"> </div>
<div id="box">
<label for="textfield">Phone</label>
<input id="textfield" type="text" size="13" maxlength="13" name="contact" />
</div>
<div id="box">
<label for="textfield" style="margin-left:15px;">Email</label>
<input id="textfield" type="text" name="email" style="margin-left:15px;"/>
</div>
<div class="clear"> </div>
<div id="box">
<label for="textfield">Date of Birth</label>
<input id="textfield" type="text" name="dob" />
</div>
<div class="clear"> </div>
<div id="box"><input type="submit" value="Register" id="submit"></div>
</form>
the php
<!DOCTYPE html>
<?php
$first = $_POST['first'];
$last= $_POST['last'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$host = 'localhost';
$db_name= 'rebeler_email';
$db_username = 'rebeler_email';
$db_password = 'callaway87';
if(isset($_POST['submit'])) {
$pdo = new PDO('mysql:host='.$host.';dbname='.$db_name, $db_username, $db_password);
$statement = $pdo->prepare('
INSERT INTO `email`(
`email`,
`first`,
`last`,
`contact`,
`dob`
) VALUES (
:email,
:first,
:last,
:contact,
:dob
)
');
$result->execute('array(
`email`=>$_POST[`email`],
`first`=>$_POST[`first`],
`last`=>$_POST[`last`],
`contact`=>$_POST[`contact`],
`dob`=>$_POST[`dob`]
)'); $email_id = $pdo->lastInsertId();
if (!result || !$customer_id) {
var_dump($pdo->errorInfo());
die('something went wrong'); // do something better to handle errors!
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<style type="text/css">
#hgtitle{
height: 50px;
width: 300px;
}
</style>
</head>
<body>
<img src="images/hgtitle.jpg" id="hgtitle">
<div id="box">
Thank you for registering.
</div>
</body>
</html>
Note the highlighted variables:
$statement = $pdo->prepare(' etc... ');
^^^^^^^^^^^
v.s.
$result->execute(array( etc... ));
^^^^^^^
If you had proper error handling in your code, including having error_reporting and display_errors turned on, you'd have seen the "calling a method of a non-object" error that the second line would produce.
These settings should NEVER be off on your development server, because they only serve to hide errors you should be seeing/fixing right away.
I'm a bit of a noob myself, but i'm not sure why you are using the POST globals on the variables for your database.
I'd just set them like this:
$host = localhost;
$db_name= rebeler_email;
$db_username = rebeler_email;
$db_password = callaway87;
Because you're not getting them from the form
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm a beginner in PHP and MySQL and I want to add values that come from an input in HTML, to a MySQL database.
I have to find some things on the Internet but this doesn't work and so I tried to learn a little bit more PHP but I still don't understand why the condition in the code below is not valid:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<title>SHAR-APP</title>
</head>
<body>
<div class='div1'>
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</div>
<?php
echo "test1";
if (isset($_POST['name'])) {
echo "test2";
$mtsqli = mysqli_connect('localhost','the_name_of_my_project','my_password');
mysqli_select_db('project_database', $msqli);
$requete = 'INSERT INTO the_name_of_the_database's_table VALUES(NULL,"' . $_POST['name'] . '","' . $_POST['code'] . '")';
$query = "SELECT * FROM the_name_of_the_database's_table";
echo $_POST['name'];
echo "test3";
}
?>
</body>
</html>
I'm on this for 3 days and I'm really blocked. Maybe I have others mistake in the PHP code. If I can do that with another language i prefer to stay on PHP because I don't want to learn too much languages. If I can do a bridge between HTML and MySQL with Python or JavaScript I'm OK to know that.
THIS PART IS GOOD but another problem is come ...
when i want to connect on my database this error message is display
C:\Users\titou>set PATH=$PATH$;C:\Program Files\MySQL\MySQL Server 8.0\bin
C:\Users\titou>mysql -h localhost -u root -p
Enter password: **********
ERROR 1045 (28000): AccŠs refus‚ pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
its in french but you can see that there is two # instand of one ('root'#'localhost')
First you need to add a Form Method
<form action="" method="POST">
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</form>
The is some kind of class that tells the browser to expect inputs and then on the button click- to treat them.
The Action="" is for initializing where to treat the given inputs.
In your case, since your php code is in the same class as the form, you can leave it blank, either way you should initialize the path you want to send those data.
The Method="POST" is just a Method for treating your data on the web. It is also more secure than GET method which it works too but it's more sensitive since all the data from the inputs it's going to be exposed also in the URL.
Furthermore, I hope you have already installed XAMPP and already created a database in MySQL.
Add the form attribute to your form and in it add a method and an action. Method is needed to tell the form to post, and action is needed to tell the form what to do when you submit.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<title>SHAR-APP</title>
</head>
<body>
<form method="post" action="">
<div class='div1'>
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</div>
</form>
<?php
if (isset($_POST['name'])) {
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$requete = 'INSERT INTO the_name_of_the_database's_table VALUES(NULL,"' . $_POST['name'] . '","' . $_POST['code'] . '")';
mysqli_query($db, $requete);
$query = "SELECT * FROM the_name_of_the_database's_table";
$Data = mysqli_query($db, $query);
var_dump[$Data];
}
?>
</body>
</html>
Now an important thing to note, never use this in a live website as it would open up your website to SQL injection. You should use prepared statements instead, but for learning purposes, this is fine.
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
My HTML on my page is eliminated after running my PHP validation on the input form.
HTML: InsertUser.php
<?php
session_start();
require_once('File Below');
echo $errors; //Errors is local variable within insertBackend.php i know. i just wanted this example to be exact compared next to my code.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script
</head>
<body>
<div class="container">
<form class="form-horizontal" role="form" method="post" action="InsertUser.php">
<div class="form-group">
<label for="txtName" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName" name="txtName" placeholder="Name" value="">
</div>
</div>
<div class="form-group">
<label for="txtPassword" class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" name="txtPassword" placeholder="Password" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="btnSubmit" name="insert_form" type="submit" value="Submit" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="btnCreate" onclick="location.href = 'createUser.php';" name="createUser" type="button" value="Create User" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="btnsame" onclick="location.href = 'InsertUser.php';" name="InsertUser" type="button" value="Insert User" class="btn btn-primary">
</div>
</div>
</form>
</div>
</body>
</html>
PHP Page: InsertBackend.php
<?php
session_start();
require_once('*DIRECTORY CONTAINING SQL CONNECTION*'); //works fine
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
if(isset($_POST['insert_form']))
{
$_SESSION['InsertUser'] = "Insert User Pass"; //This ensures i make it INTO the method
$name= "";
$password = "";
$nameInsert = mysqil_real_escape_string($_POST['txtName']);
//^This right here. mysqli_real_escape_string();
$passInsert = mysqil_real_escape_string($_POST['txtPassword']);
//^This right here. mysqli_real_escape_string();
$errors = array();
if(empty($nameInsert))
{
array_push($errors, "Please enter your Name");
}
if(empty($passInsert))
{
array_push($errors, "Please enter your password");
}
if(count($errors = 0))
{
$name = mysqil_real_escape_string($con, $_POST['txtName']);
$password = mysqil_real_escape_string($con, $_POST['txtPassword']);
$hashPass = password_hash($password, PASSWORD_DEFAULT);
$sqlInsert = "INSERT INTO Table (Name, Phash)
VALUES ('$name', '$hashPass')";
$_SESSION['VerifyHash'] = $hashPass; // Super security issue using a hashed password as a flag? I know. Just wanted visual representation of pass to compare next to my database
if ($con->query($sqlInsert) === TRUE)
{
header('location: InsertUser.php');
echo "New record created successfully <br>";
}
else
{
header('location: InsertUser.php');
echo "Error: " . $sql . "<br>" . $con->error;
}
}
}
echo "Connected"; //Flag to verify Database connection throughout usage. If it makes it here then its connected. End flag.
//CLOSE DATABASE CONNECTION
mysqli_close($con);
?>
When I run all of this together and enter test data. It arrives on: InsertUser.php. and directs properly. However it displays nothing. No code, no Sessions, no HTML. Nothing? However when I simply refresh the page without navigating anywhere it displays the entire insert form fine. And 2 of my flags display:
ConnectedInsert User Pass
I then proceed to eliminate session data. Then I just see
Connected
above the login form. After this all takes place there is no change in my Database? And no users are actually added.
Using this information I can deduce that:
my form submits to my InsertBackend.php file when called from the form.
is connected to my database appropriately.
And that my Backend script is:
Not correctly inserting into the database.
Not properly hashing my password input.
Not rendering the HTML when called back to the insert form.
I have tried really hard to figure out where exactly in this chain of events things are going awry. However I have been unable to figure out why it is not all calling properly, and why my inserts are not working at all.
I really tried finding something on here that would help me figure it out. Unfortunately I was unable to locate anything that gave me clarity. And after the last few hours i have decided to see if anyone here might have any helpful insight into my issue. Just to even have a second set of eyes on it.
This was just poorly built with no session checking. The session cannot be created if it already exists i suppose.
Using
print_r(error_get_last());
Provides me with
Array ( [type] => 8 [message] => session_start(): A session had already been started - ignoring [file] => *InsertBackend.php* [line] => 2 )
Maybe? I guess i have to test this theory. I will update code as i progress until page is all displaying and inserting into database in case anyone else runs into a similar issue down the road.
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 5 years ago.
Improve this question
This script has form where user data get entered in this form there a check box which is used to mark if that user is a team leader. And I use a another script to check if that team has team leader or not if that team has a leader the team leader check box will be disabled.
This is the form:
<?php
/**
* Created by PhpStorm.
* User: SiNUX
* Date: 4/6/2017
* Time: 3:41 PM
*/
session_start();
include_once("../iConnect/handShake.php");
$getUserRole = "SELECT * FROM userroles ORDER BY urId ASC";
$getUserRoleQuery = $dbConnect -> query($getUserRole);
?>
<html>
<head>
<title>Timer User Creation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Style Sheets -->
<link rel="stylesheet" type="text/css" href="../../CSS/main.css">
<!-- Java Scripts -->
<script language="JavaScript" type="text/javascript" src="../../jScripts/jquery-3.2.0.min.js"></script>
<script language="javascript" type="text/javascript" src="../../jScripts/svrTimeDate.js"></script>
<script language="JavaScript" type="text/javascript" src="../../jScripts/reload.js"></script>
<script language="JavaScript" type="text/javascript" src="../../jScripts/setMsg.js"></script>
<script language="JavaScript" type="text/javascript" src="../../jScripts/userCreatFunctions.js"></script>
<script language="JavaScript" type="text/javascript" src="../../jScripts/multiScript.js"></script>
<script language="JavaScript" type="text/javascript" src="../../jScripts/getIds.js"></script>
</head>
<body onload="pauseLoad4()">
<div id="divCenter" class="box">
<label id="userName">Hello <?php echo $_SESSION["fName"]." ".$_SESSION["lName"]; ?></label><br><br>
<label id="uId" hidden>1</label>
<div style="width: 166px; position: absolute; left: 642px; top: 20px; height: 44px;">
<img src="../../images/logo.png" width="142" height="33">
</div>
<label for="date">Date:</label>
<label id="date" style="margin-left: 50px;"></label><br><br>
<label for="fName">First Name:</label>
<input type="text" id="fName" name="fName" style="margin-left: 10px;" onkeyup="checkEmpty();">
<label for="lName" style="margin-left: 8px;">Last Name:</label>
<input type="text" id="lName" name="lName" style="margin-left: 10px;" onkeyup="checkEmpty();" disabled>
<label for="uName" style="margin-left: 8px;">User Name:</label>
<input type="text" id="uName" name="uName" style="margin-left: 7px;" onkeyup="checkEmpty();" disabled><br><br>
<label for="pWord1" style="margin-left: 8px;" >Password:</label>
<input type="password" id="pWord1" name="pWord1" style="margin-left: 17px;" onkeyup="checkLength();" disabled>
<label for="pWord2" style="margin-left: 8px;">Confirm Password:</label>
<input type="password" id="pWord2" name="pWord2" style="margin-left: 8px;" onkeyup="checkPass();" disabled>
<label for="uTeam" style="margin-left: 8px;">Team</label>
<select name="uTeam" id="uTeam" style="width: 170px;" onchange="teamId(this.id);enableRoles();" disabled>
<option></option>
</select>
<input type="text" name="uTeamId" id="uTeamId" hidden><br><br>
<div id="userRoles">
<label for="userRoles">User Role:</label><label for="uAttrib" style="margin-left: 250px;">User Attributes:</label><br>
<?php while ($row = $getUserRoleQuery -> fetch(PDO::FETCH_ASSOC)) { ?>
<input type="radio" class="userRoles" name="userRoles" value="<?php echo $row["urId"]; ?>" disabled><?php echo $row["userRole"]; }?>
<input type="checkbox" id="tl" name="tl" value="yes" style="margin-left: 120px;" disabled>Team Leader
</div>
<label id="msgID" hidden></label>
<div id="msg"></div>
<div id="sbmBtns">
<input type="button" value="Reset" name="reset" id="reset" class="btn" onclick="resetForm()">
<input type="button" value="Submit" name="submit" id="submit" class="btn" onclick="pauseLoad3();" disabled>
</div>
</div>
</body>
</html>
All the data collected is directed to my PHP processing page this page also has 2 parts first query executes and users details in to a database called userlogin.
Second parts is executed after the first one which take the last added id from userlogin database then it will be updated in my teams database where user ID and set a value to designate if that particular team has a leader or not.
My first part executes with out any error but the second part throws and error "invalid parameter number number of bound variables does not match number of tokens in".
I tried echoing all the data which is set in side that if function all was echoed but still this gives me that error I really can't find what I'm doing wrong please can some help me below is my PHP code.
PHP:
<?php
/**
* Created by PhpStorm.
* User: SiNUX
* Date: 4/7/2017
* Time: 12:15 PM
*/
include_once("../iConnect/handShake.php");
if (isset($_REQUEST["tl"])){
//Have to use the intermediate variable as work around to warning massage
// Strict standards: Only variables should be passed by reference
$intPword = md5($_REQUEST["pword"]);
$addUser = "INSERT INTO userlogin (uCreateDate, createdBy, fName, lName, uName, pWord, uTeam, uRole) VALUES (:uCreateDate, :uId, :fName, :lName, :uName, :pWord, :uTeam, :uRole)";
$addUserQuery = $dbConnect -> prepare($addUser);
$addUserQuery -> bindParam(':uCreateDate', $_REQUEST["udate"]);
$addUserQuery -> bindParam(':uId', $_REQUEST["uId"]);
$addUserQuery -> bindParam(':fName', $_REQUEST["fName"]);
$addUserQuery -> bindParam(':lName', $_REQUEST["lName"]);
$addUserQuery -> bindParam(':uName', $_REQUEST["uName"]);
$addUserQuery -> bindParam(':pWord', $intPword);
$addUserQuery -> bindParam(':uTeam', $_REQUEST["team"]);
$addUserQuery -> bindParam(':uRole', $_REQUEST["uRole"]);
if ($addUserQuery -> execute()){
$lastUid = $dbConnect -> lastInsertId();
if (isset($lastUid)){
$addTl = "UPDATE teams SET tlName = :tlName, tlSet = :tlSet WHERE tId = :tId";
$addTlQuery = $dbConnect -> prepare($addTl);
$addUserQuery -> bindParam(':tId', $_REQUEST["team"]);
$addUserQuery -> bindParam(':tlName', $lastUid);
$addUserQuery -> bindParam(':tlSet', $_REQUEST["tl"]);
echo $lastUid. " Last ID<br>";
echo $_REQUEST["tl"]. " TL<br>";
echo $_REQUEST["team"];
if ($addUserQuery -> execute()){
echo "1";
}else{
echo "11";
}
}
}else{
echo "10";
}
}else{
echo "3";
}
UPDATE: This is not DUPLICATE of some other post as marked look at content it was a mistake which I over saw I was binding values to the wrong query first of you people need read this before flagging. And mentioned link didn't solve my issue it's not even close to what I'm asking. Error is the same but the problem differs.
UPDATE: I just want to say sorry for making a big rant in comments section yesterday I'm really sorry acted very immature I was frustrated that led to this I'm sorry again.
The error is with this code. You are binding the params to different query object.
$addTl = "UPDATE teams SET tlName = :tlName, tlSet = :tlSet WHERE tId = :tId";
$addTlQuery = $dbConnect -> prepare($addTl);
$addUserQuery -> bindParam(':tId', $_REQUEST["team"]);
$addUserQuery -> bindParam(':tlName', $lastUid);
$addUserQuery -> bindParam(':tlSet', $_REQUEST["tl"]);
echo $lastUid. " Last ID<br>";
echo $_REQUEST["tl"]. " TL<br>";
echo $_REQUEST["team"];
if ($addUserQuery -> execute()){
echo "1";
}else{
echo "11";
}
You need to update the $addUserQuery to $addTlQuery
I have tried putting together a basic jQuery Mobile page that connects to another file on the same path of the server to send the data. I have tested the PHP by itself in the commented out part of code.
The PHP File - submit harvest.php
$dbConnection = mysqli_connect('*********', '*********', '*********', '*********');
/*
$variety = "Aji Limon";
$picked = "11";
$weight = "22";
*/
$variety = $_POST['variety'];
$picked = $_POST['picked'];
$weight = $_POST['weight'];
$query = "INSERT INTO `harvest` (`variety`, `picked`, `weight`) VALUES ('$variety', '$picked', '$weight')";
if (mysqli_query($dbConnection, $query)) {
echo "Successfully inserted " . mysqli_affected_rows($dbConnection) . " row";
} else {
echo "Error occurred: " . mysqli_error($dbConnection);
}
?>
And the html file - index.html
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<!--Mobile Device Scaling -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
</head>
<body>
<div data-role="page" id="harvest">
<div data-role="header" data-theme="b">
<h1>Chilli Harvest</h1>
</div>
<div data-role="content">
<form name="harvest" action="submitharvest.php" method="post" data-ajax="false">
<div data-role="fieldcontain">
<label for="variety" class="select" >Variety:</label>
<select name="variety" id="variety" data-theme="b">
<option value="7 Pot Red">7 Pot Red</option>
<option value="7 Pot Yellow">7 Pot Yellow</option>
<option value="Aji Limon">Aji Limon</option>
</select>
</div>
<div data-role="fieldcontain">
<label for="picked">Qty Picked:</label>
<input type="range" name="picked" id="picked" value="1" min="0" max="200" step="1" data-theme="b" data-track-theme="c"/>
</div>
<div data-role="fieldcontain">
<label for="weight">Weight (g):</label>
<input type="number" name="weight" id="weight" value="" data-theme="b" />
</div>
<div data-role="fieldcontain">
<input type="submit" value="Send" id="submit">
</div>
</form>
</div>
</div>
</body>
</html>
Having added the data-ajax="false" and even removing the entire query mobile framework I get the same problem of the php stating "Error Occurred" with no extra detail following it, I can't see anything going wring in the console either. Hopefully I'mm missing something very basic here? Please help.
So having tried a few things from the first couple of comments below - would it also be that the dbconnection being the culprit? I am using a NAS drive with personal MySQL database on it, do I need to speify the port number at the end? Currently I have:
$dbConnection = mysqli_connect('home IP Address:3306', 'username', 'password', 'databasename');
I have seen some connection strings like this where port numbers are involved:
$dbConnection = mysqli_connect('home IP Address', 'username', 'password', 'database name', 3306);
Which really adds to my confusion as I have tried all of the above and even putting the port number in "3306"... Stumped, anyone got any further ideas please?
Turns out after many hours of wasting time the web server that it was hosted on was blocking the MySQL via it's Firewall, so all my check were ok but the web host firewall was to blame and the web company have added an exclusion to allow this to work!
I've written some html code, and once i tried to place some PHP inside it, anything below this sign ?> wouldn't appear!!! I have some pictures and text that wouldn't appear unless I place it above. I'm writing with Bootstrap 2.3 and phpMyAdmin 4.10. all languages. Thank you for your time in advance.
here is my code so far:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="bootstrap/css/myStyle.css">
<title>OJRA - Registration</title>
</head>
<body>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username</h6>
<input type="text" name="username" placeholder="Your Username">
<h6>Password</h6>
<input type="password" name="usrPassowrd" placeholder="Your password">
<h6>Email</h6>
<input type="email" name="usrEmail" placeholder="Your Email"><br>
<input class = "btn btn-default" type="submit" value = "Register">
</form>
</div>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username:</h6> <input type="text" name="username" placeholder="Your username">
<h6>Password:</h6> <input type="password" name="usrPassowrd" placeholder="Your password">
<input class = "btn btn-default" type="submit" value="Sign in">
</form>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$username = $_POST['username'];
if($username == "")
{
die("cannot go empty");
header("location:index.php");
exit;
}
$password = $_POST['usrPassword'];
$email = $_POST['usrEmail'];
$query = "insert into tz_members values('$username', '$password', '$email')";
mysql_query($query) or die(mysql_error());
?>
</div>
<img src="sexymotivation.jpg" style="margin-top:-800px; margin-right:10px;" class="pull-right">
</body>
</html>
A few things have already been outlined (as answers) that do make sense, however I spotted a few typos in your inputs that will prevent your form from working, plus a few other points.
Here are a few of my recommendations:
First, this (in 2 instances) has a typo in it name="usrPassowrd" which should read as name="usrPassword" to go with your existing $password = $_POST['usrPassword'];
As I stated in my original comments:
Comment #1: die("cannot go empty"); header("location:index.php"); exit; Your header won't do anything, because it DIE()'d and that will cease to go any further.
Comment #2: What I suspect is going on is, because you've got your entire code inside one big clump, and that if certain conditions aren't met... it still wants to keep going. Now, I suggest that you put a conditional statement wrapped around your PHP....
such as if(isset($_POST['submit'])){ // PHP } then add this to your submit button name="submit" --- I also suggest you split up your form and your PHP/SQL altogether and make it submit to another page instead, with the same conditional statement I've already outlined.
If you absolutely want to execute everything in one page, try the following:
Note: I borrowed the img src from user3009875's answer also.
(Rewrite)
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="bootstrap/css/myStyle.css">
<title>OJRA - Registration</title>
</head>
<body>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username</h6>
<input type="text" name="username" placeholder="Your Username">
<h6>Password</h6>
<input type="password" name="usrPassword" placeholder="Your password">
<h6>Email</h6>
<input type="email" name="usrEmail" placeholder="Your Email"><br>
<input class = "btn btn-default" type="submit" value = "Register">
</form>
</div>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username:</h6> <input type="text" name="username" placeholder="Your username">
<h6>Password:</h6> <input type="password" name="usrPassword" placeholder="Your password">
<input class = "btn btn-default" name="submit" type="submit" value="Sign in">
</form>
<?php
// this below, will prevent a premature execution of code
if(isset($_POST['submit'])){
define('INCLUDE_CHECK',true);
require 'connect.php';
$username = $_POST['username'];
if($username == "")
{
die("cannot go empty");
// header("location:index.php"); // commented out
exit;
}
$password = $_POST['usrPassword'];
$email = $_POST['usrEmail'];
$query = "insert into tz_members values('$username', '$password', '$email')";
mysql_query($query) or die(mysql_error());
} // end brace for if(isset($_POST['submit']))
?>
</div>
<!-- commented out original img src -->
<!--
<img src="sexymotivation.jpg" style="margin-top:-800px; margin-right:10px;" class="pull-right">
-->
<!-- new img src by user3009875 in an answer given -->
<img src="sexymotivation.jpg" style="margin-top:100px; margin-right:10px;" class="pull-right">
</body>
</html>
You can't use header() there to perform a redirection because you've already outputted some HTML and PHP has flushed HTTP headers.
The reason that <img> disappeared is probably that you called die(). This function terminates the whole page at once.
If you see cannot go empty, you should check the form to make sure you posted username field. If you see some error message about MYSQL, it is mysql_query($query) that fails.
By the way, your code has a SQL Injection problem.
IT doesn't load below ?> because script fails. This is common behaviour when there is a bug.
Check tail -f /var/log/apache2/error.log on linux (terminal)
Check tail -f /var/log/apache2/error_log on max (terminal)
windows -> I have no idea... somewhere in C:\\
At this point:
die("cannot go empty");
you stop the script. The following PHP code will not be executed and the HTML will not be sent to the user.
It's possible that there is an error when executing your PHP code, that would prevent it from going through the rest of the file.
Using your browser's Developer Tools, check the contents of the HTML that was returned, it's possible that the last line could be an error message. If you have erorr_reporting off then it would write to your error log only.
/var/log/apache2/error.log is a common location for the error log file if you are using Apache on a Linux machine.
As a side note, that code you have is very dangerous, do not use data sent from the client directly in a SQL statement, you need to sanitize otherwise you make your web app vulnerable to SQL injection.
Consider using a prepared statement
http://php.net/manual/en/pdo.prepare.php
Try:
<img src="sexymotivation.jpg" style="margin-top:100px; margin-right:10px;" class="pull-right">
I think setting the top margin to -800px will cause it to disappear from the screen.
Also, make sure your image is of .jpg.