yeaaaaaa.. so so far so bad... my codes aren't working .. but im happy that there is no errors. BUT it doesnt print anything.. just blank...
$studentno = isset($_POST['studentno']);
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con->set_charset("utf8");
$result = mysqli_query($con,"SELECT * FROM data WHERE studentno = '$studentno'");
while($row = mysqli_fetch_array($result)) {
echo "<h3> Name: ". $row['last'] . " , " . $row['first']. " ".$row['middle']. " Birthdate: ". $row['birthdate'] ." Adviser: ". $row['adviser'] ."</h3>"; //these are the fields that you have stored in your database table data
echo "<h3> StudentNo.: ". $row['studentno'] . " ". " Age: " . $row['age'] ."</h3>";
echo "<h3> Track/Strand: ". $row['track'] . " ". "</h3>";
}
mysqli_close($con);
it is offline btw... just for a project..
Related
Im making a website like craigslist where people can make a posting of something they want to sell with a post title, post description, and email.
The problem:
I can pull all data from the db except for the image. When images are uploaded they are stored in C:\xampp\htdocs\uploads and the imagename is saved in the database with the rest of the info. Below is my php. Im trying to pull the name of the image from the db and use that to grab it like this
echo "<img src='uploads/".$image."' width='200'> ";
Im new to php so any tips are appreciated. Is this an ok method to store/retrieve user uploaded images?
Thanks
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql= "SELECT * FROM products";
$result = $con->query($sql);
$image = "SELECT image FROM products";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$image."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();
?>
You need to use row same for image like:
$sql= "SELECT * FROM products";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$row["image"]."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();
I am trying to insert to my database. I have tried pasting the query into MySQL Workbench and it works, but when I run the page it doesn't work.
echo "<b>DISTRICT</b></br>";
echo "crime volume : " . $crimeVolume. "<br>";
echo "population : " . $population. "<br>";
echo "crime rate : " . $crimeRate=round(($crimeVolume/($population/100000)),2) . " <br>";
echo "crime efficiency / crime clearance efficiency : " . $efficiency=($SolvedCases/$crimeVolume) ." <br>";
echo "use of force : " . $useofForce . " incidents<br>";
echo "drug seizures : " . $numdrug . " (grams/seizures)<br>";
echo "firearm seizures : " . $firearmSeizures=round(($numfire/$stationFire),2) . " <br>";
echo "<br>";
require_once('../../mysqlConnector/mysql_connect.php');
$query2= "INSERT INTO computation (crimeVolume,crimeRate,crimeSolutionEfficiency,useofForce,drugSeizures,firearmsSeizures,stationID)
VALUES ('{$crimeVolume}','{$crimeRate}','{$efficiency}','{$useofForce}','{$numdrug}','{$firearmSeizures}','0');";
echo $query2;
$result=mysqli_query($dbc,$query2);
echo $result;
Please try this example, by setting your database connection details and it should work flawless.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
// Perform query
$result mysqli_query($con,"INSERT INTO computation (crimeVolume,crimeRate,crimeSolutionEfficiency,useofForce,drugSeizures,firearmsSeizures,stationID)
VALUES ('".$crimeVolume."','".$crimeRate."','".$efficiency."','".$useofForce."','".$numdrug."','".$firearmSeizures."','0')");
// Print result
print_r($result);
// Close connection
mysqli_close($con);
?>
Note that this is not the final solution, you are better off using a proper CLASS that has some filtering and error checking as well. ( why not a framework ? ).
I hope it helps your case.
I really don't know what I'm doing here. I can display the url from the database but I can't figure out how to add the html code
$sql = "SELECT IMG_URL, Birthdate, FirstName, LastName FROM Student";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "IMG: " . $row["IMG_URL"]. " Birthdate: " . $row["Birthdate"]. " - Name: " . $row["FirstName"]. " " . $row["LastName"]. "<br>";
}
Right now, you're not outputting any HTML at all from this code. Insert an <img> tag around the outputted image:
echo "IMG: <img src=\"" . $row["IMG_URL"]. "\" /> Birthdate: " . $row["Birthdate"]. " - Name: " . $row["FirstName"]. " " . $row["LastName"]. "<br>";
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.
So, I'll post the code below. Beneath the code is where I will pose my question.
if (!empty($_SESSION['username']) && !empty($_SESSION['password']))
{
$server=mysql_real_escape_string($_POST['server']);
$teamname=mysql_real_escape_string($_POST['teamname']);
$creator=$_SESSION['username'];
$verify = mysql_real_escape_string($_POST['verify']);
$date = date("F j, Y, g:i a");
if (!empty($teamname)) {
// if ($verify == "wookie" ||
// $verify == "Wookie" ||
// $verify == "WOOKIE")
// {
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
if (mysql_query($sql,$con))
{
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
else
{
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
//} else { echo "That isn't how you spell Wookie!"; }
} else { echo "Team Name is empty, <a href='myprofile.php'>go back and give yourself a Team Name</a>"; }
} else { echo "You must be <a href='login.php'>logged in</a>!"; }
This issue is that the line "if (mysql_query($sql,$con))" goes directly to the ELSE. I'm assuming the problem lies with my $sql but I can't pinpoint where it is. Another pair of eyes would really help. Thanks a bunch!
To trace errors with mysql_query() , you should use mysql_error(). Here's an example, inspired of one of the PHP mysql_error() doc
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
mysql_query($sql,$con);
if (mysql_errno()) {
$error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$sql\n<br>";
// Your stuff
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
else {
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
Also, you should use PDO or mysqli, since mysql_* are deprecated since PHP 5.x