Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP javascript issues

Hi Everybody

here is my code that want to call javascript  function in PHP but not success,

<script type="text/javascript"> function error_block(){ document.getElementById('error_display').style.display = "block"; alert('hi'); }
</script> <?php error_reporting(0); if ($_POST) { // form submitted // *** 2: Escape all form data $username = mysqli_escape_string($conn, $_POST['username']); $password = mysqli_escape_string($conn, $_POST['password']); $first_name = mysqli_escape_string($conn, $_POST['first_name']); $last_name = mysqli_escape_string($conn, $_POST['last_name']); $gender = mysqli_escape_string($conn, $_POST['gender']); $address = mysqli_escape_string($conn, $_POST['address']); $email = mysqli_escape_string($conn, $_POST['email']); $tel = mysqli_escape_string($conn, $_POST['tel']); $is_admin = mysqli_escape_string($conn, $_POST['is_admin']);

$query = mysqli_query($conn, "SELECT * FROM members WHERE username = '$username'"); 


if (mysqli_num_rows($query) > 0){
    echo "<script> error_block(); </script>";
}
else{
    $result = mysqli_query($conn, "INSERT INTO members (username, password, first_name, last_name, gender, address, email, tel, is_admin) VALUES('$username', '$password', '$first_name', '$last_name', '$gender', '$address', '$email', '$tel', '$is_admin')");
}
// *** 3: Insert record


if (!$result) {
    exit(mysqli_error($conn)); // Show SQL error and halt the program
}

redirect('member.php');

} else { ?> <form method="post" action="member.php?action=add"> <!-- *** 1: Build HTML form --> <div id="error_display">user already exist</div> <p> Username: <input type="text" name="username" id="username" /> </p> <p> Password: <input type="password" name="password" id="password" /> </p> <p> First Name: <input type="text" name="first_name" id="first_name" /> </p> <p> Last Name: <input type="text" name="last_name" id="last_name" /> </p> <p> Gender: <input type="radio" name="gender" value="M" /> M <input type="radio" name="gender" value="F" /> F </p> <p> Address: <input type="text" name="address" id="address" /> </p> <p> Email: <input type="text" name="email" id="email" /> </p> <p> Tel: <input type="text" name="tel " id="tel" /> </p> <p> Is Admin?: <input type="radio" name="is_admin" value="1" /> Yes <input type="radio" name="is_admin" value="0" /> No </p> <p> <button type="submit" name="submit">Submit</button> </p> </form> <?php } ?>

what do i need to correct in order to make the call function work?

2 Answers

Hi Priscilla,

There are a few things that look odd in this code, however I'm not sure if it was pasted correctly. First, I'll mention that mysql_escape_string() is deprecated, and it is recommended that you use mysql_real_escape_string() instead. Using mysql_escape_string() will throw an E_DEPRECATED notice. If that doesn't help, perhaps you could repost a little code that is all indented at least 4 spaces (1 tab) so that it will format as code in the forum.

this is not deprecated. It is mysql"i"