/    Sign up×
Community /Pin to ProfileBookmark

Need PHP MYSQL help

i am using a form which post the data to MYSQL database form example if i have three inputs

a. First Name
b. Last Name
c. Age

I want to script my code in a way when some1 enter the info twice it may appear as below:


————————————

First Name Last Name queries
————————————


xyz abc 2
————————————-

But my form is showing the following output


——————————————

First Name Last Name queries
————————————

xyz abc 0

xyz abc 0
————————————-

[COLOR=”Red”]can some body help?[/COLOR]

The file<insert.php> which is used to post form data to MYSQL database is:

[QUOTE]

<?php
$con = mysql_connect(“localhost”,”myuser,”mypass”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}mysql_select_db(“mydatabase”, $con);$sql=”INSERT INTO person (id, FirstName, LastName, Age)
VALUES
(”,’$_POST[firstname]’,’$_POST[lastname]’,’$_POST[age]’)”;if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;mysql_close($con)
?>

[/QUOTE]

The file to show output on php page is:

[QUOTE]

<?php
// … the previous code
$con = mysql_connect(“localhost”,”myuser”,”mypass”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“mydatabase”, $con);

$result = mysql_query(“SELECT * FROM person”);

echo “<table border=’1′>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>”;while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘FirstName’] . “</td>”;
echo “<td>” . $row[‘LastName’] . “</td>”;
echo “<td>” . $row[‘age’] . “</td>”;
echo “<td>” . $row[‘id’] . “</td>”;
echo “</tr>”;
}
// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum – 1;
$prev = ” <a href=”$self?page=$page”>[Prev]</a> “;

$first = ” <a href=”$self?page=1″>[First Page]</a> “;
}
else
{
$prev = ‘&nbsp;’; // we’re on page one, don’t print previous link
$first = ‘&nbsp;’; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = ” <a href=”$self?page=$page”>[Next]</a> “;

$last = ” <a href=”$self?page=$maxPage”>[Last Page]</a> “;
}
else
{
$next = ‘&nbsp;’; // we’re on the last page, don’t print next link
$last = ‘&nbsp;’; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection
echo “</table>”;mysql_close($con);
?>

[/QUOTE]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@skyrider01Jan 19.2008 — Not exactly sure what you are trying to do, but you might be able to use "group by" in the select statement, eg. if you have firstname,lastname and eg.email then you could use something like this

select firstname,lastname,email,count(*) from table group by email

this would give the result you want, I believe
Copy linkTweet thisAlerts:
@taqi786authorJan 19.2008 — I want to count the no. of submitted queries

help required in the code

could u help me please!:eek:
Copy linkTweet thisAlerts:
@taqi786authorJan 19.2008 — SELECT COUNT(*) FROM Persons WHERE _____________

"________" = I don't know what to enter

I want it count the entries of visitors

whose First Name, Last Name & Email are same

Like this


------------------------------------

First Name Last Name queries
------------------------------------


xyz abc 2
-------------------------------------

Copy linkTweet thisAlerts:
@skyrider01Jan 19.2008 — ok, well then use group by how I mentioned, that will give you the result you want.

so select firstnama,lastname,count(*) as queries from persons group by email

this will group the results by email, you can group by multiple items too if you want, but email probably is something you can use.

if you want to find out more about group by, then try and so a search on google for group by sql or something similar

no need for a where clause then either, if you dont want to filter according to some other requirement

good luck
Copy linkTweet thisAlerts:
@taqi786authorJan 19.2008 — Parse error: syntax error, unexpected T_STRING in /data/apache/users/kilu.de/giftme/www/lea/test.php on line 10
Copy linkTweet thisAlerts:
@SixteaseJan 20.2008 — How about this:

[code=html]<form action="insert.php" method="post">
<input type="text" id="fname" name="fname" />
<label for="fname">first name</label><br />

<input type="text" id="lname" name="lname" />
<label for="lname">last name</label><br />

<input type="text" id="age" name="age" />
<label for="age">age</label><br />

<input type="submit" />
</form>[/code]

[code=php]<?php
$con = MySQL_connect("localhost","myuser","mypass");
$sql = "SELECT * FROM person";
$result = MySQL_query($sql, $con);
?>
<table>
<th>
<td>first name</td>
<td>last name</td>
<td>age</td>
<td>entries</td>
</th>
<?php
while ($array = MySQL_fetch_array($result)) {
print "<tr>n";
print " <td>{$array['fname']}</td>n";
print " <td>{$array['lname']}</td>n";
print " <td>{$array['age']}</td>n";
print " <td>{$array['entries']}</td>n";
print "</tr>n";
}
?>
</table>[/code]


insert.php:
[code=php]<?php
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$age = $_POST["age"];

$con = MySQL_connect("localhost","myuser","mypass");
$sql = "SELECT * FROM PERSON WHERE(first_name='$fname' AND last_name='$lname' AND age='$age')";
$result = MySQL_query($sql, $con);
$already_there = (MySQL_num_rows($result) > 0);

if ($already_there) {
$array = MySQL_fetch_array($result);
$id = $array['id'];
$entries = $array['entries'] + 1;
$sql = "UPDATE person SET entries=$entries WHERE id=$id";
}
else {
$sql = "INSERT INTO person VALUES('', '$fname', '$lname', 1)";
}
MySQL_query($sql, $con);
MySQL_close($con);

/*redirect - this must be the first output of the script
*not even an empty line can come before the initial <?php tag
*/
header('Location: http://your.site.com/');
?>[/code]

I didn't test it, so you probably will get some errors but you get the idea, right? You should also add the error checking that you usually do.
Copy linkTweet thisAlerts:
@taqi786authorJan 21.2008 — I have to create table

with name PERSON

columns

fname

lname

age

entries

Is it correct????


I am getting Parse error for line

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /data/apache/users/kilu.de/giftme/www/form/insert.php on line 9

Warning: Cannot modify header information - headers already sent by (output started at /data/apache/users/kilu.de/giftme/www/form/insert.php:9) in /data/apache/users/kilu.de/giftme/www/form/insert.php on line 26


Line 9: $already_there = (MySQL_num_rows($result) > 0);

Line 26: header('http://your.site.com/');

Could'nt know how to solve the problem

further more,

I dont know but you are not entering "select_db " statement in insert.php

is it necessary or not !
Copy linkTweet thisAlerts:
@SixteaseJan 21.2008 — Hello.

select_db IS necessary and my code was untested. It was just to give you an idea of how to do it.

The thing with headers is stated in the comment: The php code must be the very first thing in the script and no output must be given from the script before the header. So the <?php tag must be the very first 5 bytes of the file, including any whitespace and no print's or echo's before the header call.

If num_rows told you that the argument wasn't a valid MySQL result, then the MySQL_query must have failed. Check why with the MySQL_error() function.

I'm not that good with MySQL / PHP - my primary language is Perl, so I'm afraid I can only provide limited assistance.
Copy linkTweet thisAlerts:
@taqi786authorJan 21.2008 — Now i shall find my way

try to work the code

Thanks !!!!!!!!!

You are so kind!
×

Success!

Help @taqi786 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...