<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("History", $con);
/*$sql = "CREATE TABLE NewsletterSubscribers (
UserId int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(userId),
Name char[20] NOT NULL,
Email char[20] NOT NULL
)";
mysql_query($sql, $con);*/
$sql = "INSERT INTO NewsletterSubscribers (Name, Email)
VALUES ('$_POST[userName]', '$_POST[email]')";
mysql_query($sql, $con);
$result = mysql_query("SELECT * FROM NewsletterSubscribers");
[COLOR=red]while ($row = mysql_fetch_array($result)){
[/COLOR] echo $row['userid'] . " " . $row['name'] . " " . $row['email'];
echo "<br/>";
}
mysql_close($con);
?>
8 replies to this topic
#1
Posted 12 June 2011 - 07:16 AM
Hi, the following php code should print contents of table. However, i get such error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\MH2\subscribe.php on line 19. Line 19 is red. Any help would be appreciated
|
|
|
#2
Posted 13 June 2011 - 01:06 AM
Hey,
I think your php code is not the problem, check whither your mysql server is up and running
also double check your database information
try to use the PHP code below, if your connection has problem, the IF statement will show the error message
good luck :)
I think your php code is not the problem, check whither your mysql server is up and running
also double check your database information
try to use the PHP code below, if your connection has problem, the IF statement will show the error message
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("History", $con);;
$sql = "INSERT INTO NewsletterSubscribers VALUES ('$_POST[userName]', '$_POST[email]')";
mysql_query($sql);
$result = mysql_query("SELECT * FROM NewsletterSubscribers");
while ($row = mysql_fetch_array($result)){
echo $row['UserId'] . " " . $row['Name'] . " " . $row['Email'];
echo "<br/>";
}
?>
good luck :)
#3
Posted 14 June 2011 - 03:51 PM
Your issue is likely that you are enclosing your values in single quotes, '$_POST[foo]' will be the string "\$_POST[foo]" of which likely returns an error breaking the rest of your code.
Double quotes can hold single quotes, so you could wrap those around the $_POST array although you should really sanitize it prior like this:
You could also verify if there was an error by using mysql_error():
Double quotes can hold single quotes, so you could wrap those around the $_POST array although you should really sanitize it prior like this:
$username = mysql_real_escape_string($_POST['username'])
$email = mysql_real_escape_string($_POST['email'])
$sql = "INSERT INTO NewsletterSubscribers (Name, Email) VALUES ('$username', '$email')";
You could also verify if there was an error by using mysql_error():
if(mysql_query($sql, $con) == false) {
echo mysql_error();
exit;
}
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#4
Posted 14 June 2011 - 04:36 PM
It's your while loop that's wrong, you are not testing anything and therefore it generates an error!
Try replacing the row with the wile loop with this:
Try replacing the row with the wile loop with this:
while (($row = mysql_fetch_array($result)) != null){
#5
Posted 14 June 2011 - 04:38 PM
hampus.tagerud said:
It's your while loop that's wrong, you are not testing anything and therefore it generates an error!
Try replacing the row with the wile loop with this:
Try replacing the row with the wile loop with this:
while (($row = mysql_fetch_array($result)) != null){mysql_fetch_array will still produce an error, as $result is false. the while loop will already check for null.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 14 June 2011 - 04:41 PM
I remembered that when I read it through again (after posting) but didn't find any "delete post" button...
#7
Posted 24 June 2011 - 05:49 AM
The problem was in selecting database. Now i fixed it. But my code now produces new error on 27th line: Notice: Undefined index: userName in C:\wamp\www\MH2\subscribe.php on line 27. Here's the code and red 27th line which produces error:
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con){
die('Could not connect: ' . mysql_error());
}
/*$sql = "CREATE DATABASE History";
if (!mysql_query($sql, $con)){
echo mysql_error();
exit();
}*/
mysql_select_db("History", $con);
/*$sql = "CREATE TABLE NewsletterSubscribers (
UserId int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(userId),
Name char(20) NOT NULL,
Email char(20) NOT NULL
)";
if (!mysql_query($sql, $con)){
echo mysql_error();
exit();
}*/
$username = mysql_real_escape_string($_POST['userName']);
$email = mysql_real_escape_string($_POST['email']);
$sql = "INSERT INTO NewsletterSubscribers (Name, Email) VALUES ('$username', '$email')";
if (!mysql_query($sql, $con)){
echo mysql_error();
exit();
}
$result = mysql_query("SELECT * FROM NewsletterSubscribers");
while ($row = mysql_fetch_array($result)){
echo $row['userid'] . " " . $row['name'] . " " . $row['email'];
echo "<br/>";
}
mysql_close($con);
?>
Any help? (Sorry for answering so late. last week i was very busy with my exams)
#8
Posted 24 June 2011 - 06:00 AM
It says undefined index, an index usually refers to an array index so you must look for an array:
Also, you should check for this actually:
$username = mysql_real_escape_string($_POST['userName']);We can assume the invalid index is userName, as it is within the $_POST array that means that your form has not properly sent an ID with the name userName. Could you verify that the form is set up to send it?
Also, you should check for this actually:
if(isset($_POST['userName']) && isset($_POST['email'])) {
//escape here
//do database stuff here
} else {
//stop script, or else "empty" or alike could be entered in to the database even if it is empty
}
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#9
Posted 24 June 2011 - 09:27 AM
I used your code, Alexander, and it tells that form fields are not set. However, the code appears to be correct. Here it is:
public function buildSubscribeForm(){
$output = '<div id="subscribeForm" class="form">' . "\n" .
'<p class="regularFont" style="position: relative; left: 14px;">Subscribe newsletter!</p>'. "\n" .
'<form action="subscribe.php" method="post">' . "\n" .
'<ul>' . "\n" .
'<li>' . 'Name : <input type="text" name="userName" size="20"/><br/>' . '</li>' . "\n" .
'<li>' . 'E-mail : <input type="text" name="email" size="20"/><br/>' . '</li>' . "\n" .
'<li>' . '<input type="submit" value="Submit" name="submit"/>' . '</li>' . "\n" .
'</ul>' . "\n" .
'</form>' . "\n" .
'</div>' . "\n";
return $output;
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









