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.
Related
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..
<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.
I have this PHP code, which fetches data from my SQL database called "comments".
This code prints out every comment in the table:
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
}
echo '<div class = "white_line_comments"></div>';
} else {
echo "0 results";
}
This has worked fine so far, everything prints as it's supposed to.
Then I decided I wanted a way to give each individual comment some sort of identification to make them unique. I tried putting each single comment into its own div, using the SQLtable row id as id for the div.
However, when I try to access my webpage now, it tells me the website doesn't work (HTTP Error 500).
<?php
$sql = "SELECT id,name,email,number,text FROM comments";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<div class='$row[' id'].'>";
echo "<strong>ID:</strong><br> " . $row["id"] . "<br>";
echo "<strong>Navn:</strong><br> " . $row["name"] . "<br>";
echo "<strong>Email:</strong><br> " . $row["email"] . "<br>";
echo "<strong>Nummer:</strong><br> " . $row["number"] . "<br>";
echo "<strong>Melding:</strong><br> " . $row["text"] . "<br><br><br>";
echo '</div>';
}
echo '
<div class="white_line_comments"></div>';
} else {
echo "0 results";
}
Any ideas on this? I guess I must've done something wrong when including the div, but I can't figure out what!
You have an error in this line after starting while loop:
echo "<div class ='$row['id'].'>";
It should be
echo "<div class ='". $row['id'] ."'>";
You should also configure your web server/hosting/localhost to throw a PHP error.
Read this if you are on localhost or your own server:
How can I make PHP display the error instead of giving me 500 Internal Server Error
Read this if you are using shared hosting: How do I displaying details of a PHP internal server error?
echo "<div class ='$row['id'].'>";
First line in while loop should look like this
echo "<div class = '".$row['id']."'>";
or
echo "<div class = '$row['id']'>"
You have mixed different apostrophe, try this:
echo "<div class ='" . $row['id'] . "'>";
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;
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