Hi !!!
It's me again !!!
I'm following a tutorial on how to do a mailing list, and I'm getting an error when clicking on the submit button when I enter my infos...
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/free.fr/a/d/enfantsdelo/mailing/index.php on line 71
Ok, so here's my script... I cannot really figure out what's wrong with line 71. I checked the lines before, and I still cannot find it...
<?php
// subscribe newsletter
// subscribe.php
// database connection script here
// SQL database Variables
$hostname="fakehost"; // of course, they are real in the real script...
$user="fakeuser";
$pass="fakepasswd";
$dbase="fakedb";
$connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
if(!isset($mode))
{
$mode = 'index';
}
switch ($mode)
{
case 'index':
// display default form
?>
<p><strong>S'inscrire à / Se désinscrire de la Mailing List</strong></p>
<form action="<?php echo "$PHP_SELF?mode=process"; ?>" method="post">
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td>Nom:*</td>
<td><input type="text" name="name" size="20"></td>
</tr>
<tr>
<td>Email:*</td>
<td><input type="text" name="email" size="20"></td>
</tr>
<tr>
<td>Format Préféré:*</td>
<td><input type="radio" name="format" value="h" checked>HTML <input type="radio" name="format" value="t">
Texte</td>
</tr>
<tr>
<td>Mode:*</td>
<td><input type="radio" name="smode" value="subscribe" checked>
S'inscrire
<input type="radio" name="smode" value="unsubscribe">
Se désinscrire</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="C'est parti !"></td>
</tr>
</table>
</form>
<?php
break;
case 'process':
// check that all fields are filled in
if (empty($name) || empty($email))
{
die ("Ben comment je vais vous retrouver si vous ne remplissez pas tout, moi, hein ?");
}
// check for duplicate entry in database
$qCheck = "select * from subscribers where name='$name' and email='$email' ";
$rsCheck = mysql_query($qCheck);
$countCheck = mysql_num_rows($rsCheck); // the famous line 71
// determine whether user wants to subscribe or unsubscribe
if ($smode=='subscribe')
{
if ($countCheck != 0) // if entry already exists in database, do not add subscriber
{
die ("Erreur. L'enregistrement existe déjà.");
}
else {
$query = "insert into subscribers VALUES ('','$name','$email','$format')";
$successmsg = "Merci $name. Ton email, $email a été enregistré.";
}
}
else { // smode= unsubscribe
if ($countCheck == 0) // entry does not exist, cannot unsubscriber user
{
die ("Il n'y pas d'enregistrement à ce nom. Vous ne pouvez donc pas vous désinscrire ( logiquement... )");
}
else {
$query = "delete from subscribers where name='$name' and email='$email' ";
$successmsg = "Merci $name. Ton email, $email a été effacé de la base de données.";
}
}
// now execute the query
$result = mysql_query($query);
if ($result)
{
echo $successmsg;
}
break;
}
?>
here's the mysql table request:
create table subscriber (
sid int(5)not null auto_increment,
name varchar(25) not null,
email varchar(200) not null,
format char(1) not null default 'h',
PRIMARY KEY (sid))
Thanks a lot !!!
Ness