Well i use this form of
add.php
echo " <td><form action='view_ticket.php' method='get'>
<input type='hidden' id='itemid' name='itemid' value='". $row["id"] ."'/>
<input type='submit' value='Buy' class='btn-link'/>
</form></td>";
echo "</tr>";
And i got this
view_ticket.php
<?php
session_start();
$conn = mysqli_connect("localhost", "localhost", "localhost",
"localhost");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['itemid'];
$sql = "SELECT id,username FROM tickets WHERE id = '" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['username'] = $_SESSION['username']) {
}
else
{
header("location:/quickshops/buyer/tickets.php");
}
}
}
?>
the only problem i guess that $id = $_GET['itemid']; gets out empty idk how
I checked the source code of add.php , the ids in hidden area are real ,true, and numbers
Output $_GET at each step to see where you loose it, for instance
echo 'About to look for id "' . htmlspecialchars($id) . '"<br>';
$result = $conn->query($sql);
echo 'Just executed SQL query<br>';
var_dump($_GET); echo '<br>';
if ($result === false) {
echo 'SQL Query Error!<br>';
var_dump($_GET); echo '<br>';
} else if ($result->num_rows > 0) {
echo $result->num_rows . ' row(s) found<br>';
var_dump($_GET); echo '<br>'; /* do something */
} else {
echo 'No rows round<br>';
var_dump($_GET); echo '<br>';
}
Related
I don't know what's going on, I selected the price in the table, and when I use it it's fine. In the <form> the $seller and $_SESSION['username'] also fine, but in the buy_item.php when I try to calculate $newAmount = ($row["amount"] - $price); and tried to see the value it seems like $price became 0$.
PAGE.PHP
The sql
$sql = "SELECT seller,price FROM stuff ";
in the table (ALL informations appears fine)
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>". $row["price"] . "</td>";
echo "<td>". $row["seller"] . "</td>";
echo " <td><form action='buy_item.php' method='post'>
<input type='hidden' id='seller' name='seller' value='".$row['seller']."'/>
<input type='hidden' id='price' name='price' value='".$row['price']."'/>
<input type='hidden' id='buyer' name='buyer' value='". $_SESSION['username'] ."'/>
<input type='submit' value='Buy'/>
</form></td>";
echo "</tr>";
BUY_ITEM.PHP
<?php
session_start();
$conn = mysqli_connect("hi", "hi", "hi", "hi");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$seller = mysqli_real_escape_string($link, $_REQUEST['seller']);
$price = mysqli_real_escape_string($link, $_REQUEST['price']);
$buyer = mysqli_real_escape_string($link, $_REQUEST['buyer']);
$sql = "SELECT username,amount FROM users WHERE username = '" .
$_SESSION['username'] . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["amount"] >= $price) {
$newAmount = ($row["amount"] - $price);
echo ("$newAmount");
}
else
{
echo "Records added successfully.";
}
}
} else { echo "0 results"; }
$conn->close();
?>
the $newAmount always show up = $amount cause $price always become = 0
I'm going to take a punt and say that $price has a hidden character or the like in it.
PHP practices type juggling. Internally PHP is a typed language which casts the real type as required.
$_REQUEST provides the value of $price as a string.
When you use it in a subtraction PHP casts it from a string to an integer. Something goes wrong in the cast, it doesn't look like a number, so PHP makes it zero. Your results then occur.
Printing it out with var_dump() as #mr-glass suggested will make this clear.
I'm attempting to shuffle the users that I have in my database and then output the first and last name of those results. I have PHP error coding in this file and it is not throwing any errors. Just nothing is outputting not even..
if ($shuffle_firstname == true) {
echo $shuffle_firstname . $shuffle_lastname;
} else {
echo "No users have been registered yet.";
}
Does anyone see what I'm doing wrong?
$con = mysqli_connect("localhost", "root", "", "db");
$shuffle_run = mysqli_query($con,"SELECT * FROM users WHERE `group`= 3");
$shuffle_numrows = mysqli_num_rows($shuffle_run);
if( $shuffle_numrows > 0) {
while($shuffle_row = mysqli_fetch_assoc($shuffle_run)){
$shuffle_id = $shuffle_row['id'];
$shuffle_firstname = $suffle_row['firstname'];
$shuffle_lastname = $shuffle_row['lastname'];
$shuffle_username = $shuffle_row['username'];
$shuffle_email = $shuffle_row['email'];
if ($shuffle_firstname == true) {
echo $shuffle_firstname . $shuffle_lastname;
} else {
echo "No users have been registered yet.";
}
}
}
if(isset($_POST['shuffle'])) {
$shuffle_row = array();
shuffle($shuffle_row);
foreach ($shuffle_row as $shuffle) {
echo $shuffle_firstname . " " . $shuffle_lastname;
}
}
?>
<input type="submit" value="Shuffle" name="shuffle">
You are overwriting your results with an empty array, then you use shuffle on an empty array, then you're looping the empty array using foreach. I've cleaned up the code, could you try this?
<?php
$con = mysqli_connect("localhost", "root", "", "db");
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");
echo 'Normal results: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
if (isset($_POST['shuffle'])) {
shuffle($array);
echo 'Shuffled results: <br>';
foreach ($array as $result) {
echo $result['firstname'] . ' ' . $result['lastname'] . '<br>';
}
// echo $results[0]['firstname'] . ' ' . $results[0]['lastname']; // To display one random result
}
?>
<form method="post">
<input type="submit" value="Shuffle" name="shuffle">
</form>
When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs
I have a form I'm using to send multiple fields with a similar name but using square brackets to send them as an array with the key being 'id'. I am able to loop through successfully using a foreach loop but my insert query fails to make any changes in the db.any clues as to why?
here is the code from the form:
$artist = mysql_fetch_array($allartists);
$allmusic = get_music_for_artist($artist_id);
$id = $music['id'];
while ($music = mysql_fetch_array($allmusic)) {
echo "<td class=\"td20 tdblue\">
<input name=\"song_title[" . $id . "]\" type=\"text\" value=\"" . $music['song_title'] . "\" />
</td>";
}
and here is the code on my form processor
foreach ($_POST['song_title'] as $id => $song) {
$query = "UPDATE music SET
song_title = '{$song}'
WHERE id = $id ";
if (mysql_query($query, $connection)) {
//Success
header("Location: yourmusic.php?pid=3");
exit;
} else {
//Display error message
echo "update did not succeed";
echo "<p>" . mysql_error() . "</p>";
}
}
Try this.
Also watch for security http://www.phptherightway.com/#data_filtering
foreach ($_POST['song_title'] as $id => $song) {
$id = (int) $id;
$song = mysql_real_escape_string($song);
$query = "UPDATE music SET
song_title = '$song'
WHERE id = $id LIMIT 1;";
if (mysql_query($query, $connection)) {
//Success
header("Location: yourmusic.php?pid=3");
exit;
} else {
//Display error message
echo "update did not succeed";
echo "<p>" . mysql_error() . "</p>";
}
}
I am trying to make the different the different rows have line breaks but its not working.
How is this done!? Please check my code below
Thanks guys!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
{
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '$search%' AND lastname LIKE '$searchterm'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}
mysql_free_result($result);
?>
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
Beats me what you find so hard about it:
while ($row = mysql_fetch_array(...)) {
echo ...
echo '<br>';
}