0

I'm not good in PHP, I have problem to process with my sign form to using the php script, the server had returned me a message below:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/tz005/public_html/test2/email-activation-script.php on line 11

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tz005/public_html/test2/email-activation-script.php on line 19
Failed to sent email activation code

Can anyone check what is the problem on my script.Here is my script:

<?php
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$postcode=$_POST['postcode'];
$db_host = "server";
$db_name = "table";
$db_use = "user";
$db_pass = "pass";
$link = mysqli_connect($db_host, $db_use, $db_pass);
mysqli_select_db($db_name, $link);
$chars = array("1","2","3","4","5","6","7","8","9");
$length = 5;
$textstr = "";
for ($i=0; $i<$length; $i++) {
   $textstr .= $chars[rand(0, count($chars)-1)];
}
$query = "INSERT INTO email_activation (username, password, email, postcode, activation) VALUES ('$username','$password','$email','$postcode','$textstr')";
$result = mysqli_query($query);
if ($result) {
echo "Sign up successfully,please check your email for activation code.<br>";
$mail_to="$email";
$mail_subject="Email Activation";
$mail_body = "This is the email to activate your account.\n";
$mail_body.="Your activation code is: $textstr \n";
$mail_body.="Click the following link to activate your account.\n";
$mail_body.="<a href='activation-form.php?username=$username&activation=$textstr'>Click here</a>";
$sent = mail($mail_to,$mail_subject,$mail_body, "From: freecycle@greenwich.co.uk");
echo "$mail_to<br><b>$mail_subject</b><br><br>$mail_body";
}else{
echo "Failed to sent email activation code";
}
?>
Jenz
  • 8,280
  • 7
  • 44
  • 77

4 Answers4

0

You can pass db name in connection string so need to select db with function try

$link = mysqli_connect($db_host, $db_use, $db_pass, $db_name);
$result = mysqli_query($link, $query);

For more :- http://www.php.net/manual/en/book.mysqli.php

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

You have the arguments the wrong way around for the mysqli_select_db() call. The first argument should be the link, the second being the string database name. So change to:

mysqli_select_db($link, $db_name);

Secondly, In the query call, you need to pass the link, not just the query:

$result = mysqli_query($link, $query);

References:

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Mysqli doesn't work the way the Mysql functions work, You need to define the connection in a variable to make use of the function.

However you could just change your functions to mysql (if installed) and it'll work.

Basicly:

mysqli_select_db

Becomes

mysql_select_db

Etcetera, this will probably be fine for your project. If you get more at ease with PHP look at the classes to handle your Database, I guess you'll find those yourself.

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
  • This isn't good advice, see this: http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql and http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php – Joe May 20 '14 at 11:59
  • True I guess. He is using the mysqli functions as mysql functions though. – Mathijs Segers May 20 '14 at 12:01
0

change

mysqli_select_db($db_name, $link);

to

mysqli_select_db($link, $db_name);

also change

$result = mysqli_query($query);

to

$result = mysqli_query($link, $query);
fortune
  • 3,361
  • 1
  • 20
  • 30