Input regarding this PHP script to query a mysql database? - php

I was using this script and everything was great except for the fact that when the actual search was executed and "No Results" was the answer, I wanted the script to display such.
When doing research to see where the FAIL was I discovered that I should be using MySQLi. I have been at this script for 2 days and I seem to be getting further instead of closer. A little help here fellas?
What I am using:
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
include 'dbclose.php';
}
mysql_close($con);

You can retrieve the count of the rows with:
mysqli_num_rows($page_query);
Simply verify that it is >0 to chose what to display, the error message or the results

Just add a condition to display something if your query returns 0
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
if (mysqli_num_rows($page_query) > 0){
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
} else {
echo 'Your search returned 0 results';
}
include 'dbclose.php';
}
mysql_close($con);

Related

Php page not displaying

I am having a problem displaying a JOIN statement. When I add
WHERE id = " . $team_id;
The information that is on the database will not display, but when I remove that line the information will correctly join and display on the "teaminfo.php " page, but it will display all of the data instead of the data that is unique to that id. Also when I remove the JOIN the the data that is unique to the id will display. Can anyone tell me whats wrong here. Any help will be great. Than you.
teaminfo.php
<html>
<head>
<title>Team Info page</title>
</head>
<body>
<?php
include 'connect.php';
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
if ($result = mysqli_query($mysqli, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
// Write the data of the team
echo "<br />";
echo "Pit scouting";
echo "<dt>Team:</dt><dd>" . $row["pteam_number"] . " " . $row["pteam_name"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["pauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["pdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["pobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["pobjWithProblem"] . "</dd>";
echo "<dt>Can they shoot? If yes from where and how acc</dt><dd>" . $row["pshoot"] . "</dd>";
echo "<dt>Extra Notes about their robot?</dt><dd>" . $row["pdrive"] . "</dd>";
echo"<br />";
echo "Field Scouting ";
echo "<dt>Team Number:</dt><dd>" . $row["fteam_number"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["fauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["fdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["fobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["fobjWithProblem"] . "</dd>";
echo "<dt>Shots taken</dt><dd>" . $row["fshots_taken"] . "</dd>";
echo "<dt>Shorts made</dt><dd>" . $row["fshots_made"] . "</dd>";
echo "<dt>Extra Notes</dt><dd>" . $row["fnotes"] . "</dd>";
}
mysqli_free_result($result);
}
// Close the database connection
mysqli_close($mysqli);
?>
<p>Return to the list</p>
</body>
</html>
Palmetto.php
<?php
include 'connect.php';
// SQL query
$query = "SELECT * FROM pitscouting ORDER BY pteam_number";
if($result = mysqli_query($mysqli, $query)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$name = $row['pteam_number'] . " " . $row['pteam_name'];
// Create a link to teaminfo.php with the id-value in the URL
$strLink = "<a href = 'teaminfo.php?id= " . $row['id'] . "'>" . $name . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $query. " . mysqli_error($mysqli);
}
// Close connection
mysqli_close($mysqli);
?>
If your tables both have an ID field you will have to specify which table you want to get the data from.
WHERE pitscouting.id = " . $team_id;
or
WHERE fieldscouting.id = " . $team_id;
Please do mention the sql injection in you're code
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
please take a look at prepared statements, to prevent sql injections in youre code
Try putting an alias.
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting p
JOIN fieldscouting f
ON p.pteam_number = f.fteam_number
WHERE p1.id = " . $team_id;

Deleting a row with Php & MySQL

I am new to php and SQL and just toying around with a project for my own understanding of accessing, updating and deleting data from my Database.
I have managed to show the selected data, create a button to delete a specific Id but really needing some assistance with deleting the selected row or record instead of hard coding in the ID in my delete php script.
Here is an example of my script:
<?php
$sql = "SELECT id, firstname, lastname, joinDate FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<form action='deleteMember.php' method='POST'>
<button type='submit'>Delete</button>
</form>
</span>" . " " .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Here is the delete.php
<?php
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='6' ";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
what I would like it to do is, delete the row from which you hit the delete button from and not just delete the row I have specified in the delete.php script. I understand HOW it should work by posting the id but not sure how to do it.
Do like this
<?php
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
<?php
$sql = "SELECT id, firstname, lastname, joinDate FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span><a href='deleteMember.php?id=".$row['id']."'>Delete</a></span>" .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
in place of your form use this
DELETE
and in your delete query must be like below
$sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";
or stay in your post form with:
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<form action='deleteMember.php' method='POST'>
<input type='hidden' name='myid' value='".$row['id']."' />
<button type='submit'>Delete</button>
</form>
</span>" . " " .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
And in your delete.php :
<?php
$id=(int) $_POST['myid'];
// sql to delete a record
$sql = "DELETE FROM customers WHERE id=".$id;
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
No need to add extra form element for Delete or Edit purpose. Try this way to pass the id of row for Eelete or Edit operation
while($row = $result->fetch_assoc())
{
$id=$row['id'];// capture your row id & pass to your delete & edit
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<a href='deleteMember.php?id=<?=$id;?>'>Delete</a>
</span>" . " " .
"<span class='editMember'>
<a href='editMember.php?id=<?=$id;?>'>Edit</a>
</span>" .
"<br>
</div>";
}
EDIT:
Then catch the id on your relevant page for your operation.
//deleteMember.php
<?php
$id=$_GET['id'];
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='".$id."'";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Note: Please Use Prepared Statements of PDO or MYSQLi instead to avoid SQL Injection and manual escaping.

Ajax pagination in php mysql

I copied this code from a website for using ajax jquery pagination in php with mysql. I have a mysql table with 5 columns. I want to show all columns of the mysql table - not only two.
I edited the while loop but I cannot insert more columns. Please help me - I am new here and it may be difficult to understand my question. sorry in advance.
$query_pag_data = "SELECT * from ebook LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
I have linked all code
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html
You only show $row['id'] and $row['title'], just add some extra with the row names ;)
Example:
$msg .= "<li><b>" . $row['id'] . "</b> <i>" . $row['another_row_name'] . "</i>" . $htmlmsg . "</li>";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</b> " . $row['somefield'] ."</li>";
}
The different values are accessed by the $row array so it depends on what your column names are for the 5 columns.
you might what something like
$msg .= "<li><b>" . $row['colname1'] . "</b> " . $row['colname2'] . " " . $row['colname3'] . <insert more columns here> . "</li>" ;

PHP / MySQL - List who recruited whom

i'am working on a social project and we've just startet the registration for members.
Our members could recriute new members, so we have a database where is a field "recruit_by".
The DB fiels looks like this:
id | name | email | code | recruit_by
We now want to generate a list of the structure, who recruited whom on all levels.
I've tried to get this done, but it seems my skills are to less to get this done.
I get a list, but this is totaly unsorted :-(
Thanks for your help!
<?PHP
mysql_connect("www.mysqlserver.net", "database1", "password") or die(mysql_error());
mysql_select_db("project_db1") or die(mysql_error());
echo "<ul>";
$result = mysql_query("SELECT * FROM registration") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<li class=\"level0\">" . $row['id'] . " - " . $row['name'] . " - " . $row['email'] . " - " . $row['recruit_by'] . "</li>";
// 1. Level
$result2 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row['id']) or die(mysql_error());
while($row2 = mysql_fetch_array($result2))
{
echo "<li class=\"level1\">1. " . $row2['id'] . " - " . $row2['name'] . " - " . $row2['email'] . " - " . $row2['recruit_by'] . "</li>";
// 2. Level
$result3 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row2['id']) or die(mysql_error());
while($row3 = mysql_fetch_array($result3))
{
echo "<li class=\"level2\">2. " . $row3['id'] . " - " . $row3['name'] . " - " . $row3['email'] . " - " . $row3['recruit_by'] . "</li>";
// 3. Level
$result4 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row3['id']) or die(mysql_error());
while($row4 = mysql_fetch_array($result4))
{
echo "<li class=\"level3\">3. " . $row4['id'] . " - " . $row4['name'] . " - " . $row4['email'] . " - " . $row4['recruit_by'] . "</li>";
// 4. Level
$result5 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row4['id']) or die(mysql_error());
while($row5 = mysql_fetch_array($result5))
{
echo "<li class=\"level4\">4. " . $row5['id'] . " - " . $row5['name'] . " - " . $row5['email'] . " - " . $row5['recruit_by'] . "</li>";
}
}
}
}
}
echo "</ul>";
?>
First, you are selecting all registrations, that results all the recuited users also.
I would suggest selecting only non-recuited users, assuming recruit_by is NULL, when this user is not recruited:
$result = mysql_query("SELECT * FROM registration WHERE recruit_by IS NULL") or die(mysql_error());
Secondly it is good to structure second level of recruits in another <ul></ul> tags like this:
<ul>
<li>User #1
<ul>
<li>User #103, recruited by user #1</li>
<li>User #142, recruited by user #1</li>
<li>User #93, recruited by user #1
<ul>
<li>User #992, recruited by user #93</li>
</ul>
</li>
</ul>
</li>
</ul>
This already gives you much better structure to work with and you can easily loop it in you PHP code (DRY - don't repeat yourself).
<?php
function createTree($level = 0, $recruiter_id = 0) {
$return = "";
if (!$recruiter_id) {
$results = mysql_query("SELECT * FROM registration WHERE recruit_by IS NULL") or die(mysql_error());
} else {
$results = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $recruiter_id) or die(mysql_error());
}
// Check if there is any recruits at all?
if (pg_num_rows($results) > 0) $return .= "<ul>\n";
else return "";
while ($row = mysql_fetch_array($results)) {
$return .= "<li class=\"level".$level."\">\n";
$return .= $level.". " . $row['id'] . " - " . $row['name'] . " - " . $row['email'] . " - " . $row['recruit_by']."\n";
// Add sub-recruits
$return .= createTree($level+1, $row['id']);
// Finish up the <li>
$return .= "</li>\n";
}
$return .= "</ul>\n";
return $return;
}
?>

How can I have my php return only a certain id from mysql

How can I have my php page only return rows with a certain id. I am working on a webpage set up like a blog, i post using mysql, i it to only show entries with the id of 1, so i don't have to worry about deleting old posts or having 100 posts on 1 page.
<?php
include ("includes/includes.php");
$blogPosts = GetBlogPosts();
foreach ($blogPosts as $post)
{
echo "<div class='post'>";
echo "<h2>" . $post->title . "</h2>";
echo "<p>" . $post->post . "</p>";
echo "<br />";
echo "<span>Posted By: " . $post->author . "&nbsp Posted On: " . $post->datePosted . "&nbsp Tags: " . $post->tags . "</span>";
echo "</div>";
}
?>
$result = mysql_query("SELECT * FROM entries WHERE id=1");

Categories