Jump to content

Tomy02

Members
  • Posts

    5
  • Joined

  • Last visited

Tomy02's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Got it! What a great community we have here, kudos mikosiko.
  2. Im new to prepared statement and after a lot of research and reading, i still cant get it right. I have created a small database with user_id auto, name, lastname, and tried to make an insert with prepared statement. I have tried it several ways but still cant spot the error. Here is my 1 code. <?php $mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem'); if (!($stmt = $mysqli->prepare("INSERT INTO pps(name) VALUES (?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } /* Prepared statement, stage 2: bind and execute */ $username = "john"; if (!$stmt->bind_param($username)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } $stmt->close(); ?> This above code, gives me Warning: Wrong parameter count for mysqli_stmt::bind_param() in line 9. Binding parameters failed: (0) Execute failed: (2031) No data supplied for parameters in prepared statement. Note: Line 9 is this : $username = "john"; Then i tried this code which also fails. <?php $mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem'); $query = "INSERT INTO pps (name, lastname) VALUES (?,?,)"; $stmt = $mysqli->prepare($query); $val1 = 'John'; $val2 = 'Lastname'; $stmt->bind_param("ss", $val1, $val2); /* Execute the statement */ $stmt->execute(); $val1 = 'Mark'; $val2 = 'Lastnamel'; /* Execute the statement */ $stmt->execute(); /* close statement */ $stmt->close(); $mysqli->close(); ?> This code results in Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\PreparedStatement\test.php on line 9. How can i correct this ?
  3. Dont worry about it, appreciate all input.
  4. You are right KevinM1, i got it to work with this line. return mysql_result(mysql_query("SELECT (user_id) FROM users WHERE username='$username'"), 0, 'user_id'); Was getting a boolean error with that line earlier but it works now. Thanks everyone.
  5. Im developing a site a that has login function, i have about 5 users with different user id. The problem is the code im using logs all the users as the first user in my database table, this means im essentially logged in as a another user. // function that gets users id from table users function user_id_from_username( $username ){ $username = sanitize( $username ); // do the query first $result = mysql_query( "SELECT user_id FROM users WHERE username = '$username'" )or die("Could not perform select query - " . mysql_error());;;; // if there was an error, or no results, $result will be FALSE. // if $result is not false, then you know there was a row returned $num_rows = mysql_num_rows($result); if($num_rows == 1) { return true; } else { return false; } } // function that checks the credentials and should return the correct user_id. function login($username, $password) { $user_id = user_id_from_username($username); $username = sanitize($username); $password = ($password); $query = mysql_query("SELECT COUNT(`user_id`) FROM guys WHERE username='$username' AND password='$password'"); return (mysql_result($query, 0) == 1) ? $user_id : false; } // finally logging the user and redirecting $login = login($username, $password); if ($login == false) { $errors[] = 'This username and password combination is incorrect'; }else { $_SESSION ['user_id'] = $login; header ('Location: index.php'); exit(); } function logged_in() { return (isset($_SESSION['user_id'])) ? true : false; } How can i log in the others users with the correct user id, and not just the first user ?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.