You are viewing our Forum Archives. To view or take place in current topics click here.
what is up with this [php]
Posted:

what is up with this [php]Posted:

AndrewHuby
  • New Member
Status: Offline
Joined: Mar 13, 201311Year Member
Posts: 41
Reputation Power: 1
Status: Offline
Joined: Mar 13, 201311Year Member
Posts: 41
Reputation Power: 1

<?php
mysql_connect("localhost","root","");
mysql_select_db("login");
$username = $_POST['username'];
$password = $_POST['password'];
$passwordch = $_POST['passwordch'] ;
$email = $_POST['email'];
$emailC = $_POST['emailc'];


if($password != $passwordch){
echo "please retype your password";
}else{

if($email != $emailC){
echo "please retype your email";
}else{

$sql = mysql_query("INSERT INTO `users`(`id`, `username`, `password`, `email`) VALUES (,$username,$password,$email)");



}
}

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

?>

it dosnt wont to submit to the database
#2. Posted:
Nic
  • Retired Staff
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Motto: I've been watching you all day.
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Remove the "id" in your mysql query and the first comma in the values, like so:
$sql = mysql_query("INSERT INTO `users` (username, password, email) VALUES ($username, $password, $email)");


See what happens now. Also, if that doesn't work, what error message do you get?
#3. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Ok well first of what's the point in escaping the users input after entering into a mysql query? kinda ruins the whole point of doing so, also there is problems with your mysql query itself and your code looks awfully messy, Definately look into mysqli or PDO.

Lets clear this up properly:

 <?php
define("HOST", "localhost");
define("USER", "root");
define("PASSWORD", "");
define("DATABASE", "login");
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if(mysqli_connect_errno()){
    printf("DB error: %s", mysqli_connect_error());
    exit();
}

 $username = $_POST['username'];
 
 $password = $_POST['password'];
 $passwordch = $_POST['passwordch'] ;
 
 $email = $_POST['email'];
 $emailC = $_POST['emailc'];
 
 if($password != $passwordch || $email != $emailC){
   echo "do not match";
 }else{
   if($stmt = $mysqli->prepare("INSERT INTO users (username, password, email) VALUES (?,?,?)")){
      $stmt->bind_param('sss', $username, $password, $email);
      $stmt->execute();
      $stmt->close();
   }else{
      echo "error executing query";
   }
 }   
 ?>


Adding to this you don't actually need to insert into the id if it's set to auto increment as mysql will do that all for you, the same applies to time-stamps set to current time-stamp as default. I'm just trying to promote good practice.


Last edited by iyop45 ; edited 1 time in total
#4. Posted:
Nic
  • Retired Staff
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Motto: I've been watching you all day.
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
iyop45 wrote
Ok well first of what's the point in escaping the users input after entering into a mysql query? kinda ruins the whole point of doing so, also there is problems with your mysql query itself and your code looks awfully messy, Definately look into mysqli or PDO.

Lets clear this up properly:

 <?php
define("HOST", "localhost");
define("USER", "root");
define("PASSWORD", "");
define("DATABASE", "login");
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if(mysqli_connect_errno()){
    printf("DB error: %s", mysqli_connect_error());
    exit();
}

 $username = $_POST['username'];
 
 $password = $_POST['password'];
 $passwordch = $_POST['passwordch'] ;
 
 $email = $_POST['email'];
 $emailC = $_POST['emailc'];
 
 if($password != $passwordch && $email != $emailC){
   echo "do not match";
 }else{
   if($stmt = $mysqli->prepare("INSERT INTO users (username, password, email) VALUES (?,?,?)")){
      $stmt->bind_param('sss', $username, $password, $email);
      $stmt->execute();
      $stmt->close();
   }else{
      echo "error executing query";
   }
 }   
 ?>


Adding to this you don't actually need to insert into the id if it's set to auto increment as mysql will do that all for you, the same applies to time-stamps set to current time-stamp as default. I'm just trying to promote good practice.

In case he's actually going to use your code, you might want to change the logic operator AND to OR at the end as it will still execute the query if only one of those (email or password) is incorrect.
#5. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Nicasus wrote
iyop45 wrote
Ok well first of what's the point in escaping the users input after entering into a mysql query? kinda ruins the whole point of doing so, also there is problems with your mysql query itself and your code looks awfully messy, Definately look into mysqli or PDO.

Lets clear this up properly:

 <?php
define("HOST", "localhost");
define("USER", "root");
define("PASSWORD", "");
define("DATABASE", "login");
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if(mysqli_connect_errno()){
    printf("DB error: %s", mysqli_connect_error());
    exit();
}

 $username = $_POST['username'];
 
 $password = $_POST['password'];
 $passwordch = $_POST['passwordch'] ;
 
 $email = $_POST['email'];
 $emailC = $_POST['emailc'];
 
 if($password != $passwordch && $email != $emailC){
   echo "do not match";
 }else{
   if($stmt = $mysqli->prepare("INSERT INTO users (username, password, email) VALUES (?,?,?)")){
      $stmt->bind_param('sss', $username, $password, $email);
      $stmt->execute();
      $stmt->close();
   }else{
      echo "error executing query";
   }
 }   
 ?>


Adding to this you don't actually need to insert into the id if it's set to auto increment as mysql will do that all for you, the same applies to time-stamps set to current time-stamp as default. I'm just trying to promote good practice.

In case he's actually going to use your code, you might want to change the logic operator AND to OR at the end as it will still execute the query if only one of those (email or password) is incorrect.


Ah, I had a bit of a derp moment there my bad. It should be fine now.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.