Cant SELECT decimal Value - php

I know this question might sound simple to you, as if it case sensitive issue, However it is not. My problem is that when I try to grab a Decimal value(10,2) it returns 0.00 but the rest of the rows it selected are fine. Here is my code:
This grabs the rows, most of them work and ALL of them are properly typed with Caps in the right places:
$query = "SELECT * FROM invoice WHERE id='$id'";
$result = mysql_query($query) or die("Couldn't execute query");
while ($row = mysql_fetch_array($result)) {
$DateCreated = $row['DateOfCreation'];
$id = $row['id'];
$Description = $row['Description'];
$ProductCode = $row['ProductCode'];
$VatRate = $row['VatRate'];
$PriceExVat = $row['PriceExVat'];
$Status = $row['Status'];
}
This outputs the variable:
<input type="text" name="priceExVat" size="10" id="priceExVat" value="<? echo $PriceExVat; ?>"/>

I would guess that the PHP server has short tags disabled. Try replacing
<? echo $PriceExVat; ?>
with
<?php echo $PriceExVat; ?>

Related

PHP passing Array

I have two php page.
In the first I have looping checkbox array :
<td><input type="checkbox" name="cek[]" value=" <?php echo "$kodeinventarisit" ?>"></td>`
Then i submit form from page one to page two :
<?php
include 'koneksi.php';
$cek = $_POST['cek'];
$jumlah_dipilih = count($cek);
for($x=0;$x<$jumlah_dipilih;$x++){
$jojo = $cek[$x];
$coba = "select * from msstok where kodeinventarisit = '$jojo' ";
$cobaquery = mysql_query($coba);
$hasil = mysql_fetch_array($cobaquery);
$jenis = $hasil['jenis'];
?>
<input name="kode" type="text" id="license" value="<?php echo htmlentities($jenis) ; ?>" readonly="readonly" />
<?php
echo "$jojo";
}
?>
The problem is in the sql query return nothing, I try echo "$jojo" and it's print the value but in the text field is empty..
Does anyone have suggestions on how to fix this?
Thank You Very Much
1
What you are doing is bad.
Load your data before your loop and loop every result to print them.
2
Protect your sql request from injection.
Connect
$db = new mysqli("","root","","");
Prepare your request
$sql = "select * from msstok where kodeinventarisit = ? ";
$res = $db->prepare($sql);
$res->bind_param("sssd",$jojo);
Get results
$res->execute();
Documentation : http://php.net/manual/fr/book.mysql.php
If you want to pass the array you need to check if arrive in you second page.
<pre>
print_r($_POST['cek']);
</pre>
Now, if arrive here, you can read the values like this:
<?php
// If is array(), then you can go to loop
if(is_array($_POST['cek']))
{
// Run the loop
foreach($_POST['cek'] as $value)
{
// Show values per line
echo $value. "<br/>";
}
}
?>
You can read only 1 value of your array
<?php echo $_POST['cek'][0]; ?>
<?php echo $_POST['cek'][1]; ?>
<?php echo $_POST['cek'][2]; ?>
Conclusion
You can't pass array to SQL in query. If you want to use it, this is the only way with implode.
$coba = "SELECT * FROM msstok WHERE kodeinventarisit IN (".implode(',', $jojo).")";
$records = mysql_query($coba, $connection);
while ($row = mysql_fetch_array($records)) {
echo "Name: " . $rows['name'] . "<br />"; // replace the name for column you want
}

PHP/SQL fetch query data

I'm having a problem in this simple SQL/PHP query...
<?php
$course=$row['course'];
include('../db.php');
$cat=$row['cat'];
$result = mysql_query("SELECT * FROM question WHERE course='$course' AND cat='$cat'");
while($row = mysql_fetch_array($result))
{
echo $row['question'].'?<br>';
$qid=$row['qid'];
echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
echo '<select name="answer[]">';
echo '<option>Select Answer></option>';
$resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 4");
while($rowik = mysql_fetch_array($resultik))
{
echo '<option>';
echo $rowik['opt'];
echo '</option>';
}
echo '</select><br><br>';
}
?>
Basically, this is a online examination. I want to display all the questions if the student will login. And the questions will be order/arrange according by their course. But eventually, there's no display at all. Not even a single letter will display.
Any help would be appreciated. Thank you so much.
In this there must some POST or GET values to get the course and cat which means
$course=$row['course'];
$cat=$row['cat'];
Since the $row is empty this is the case it will not display anything. Check with isset() like following
$course = isset($row['course']) ? $row['course'] : 'COURSE';
$cat = isset($row['cat']) ? $row['cat'] : 'CAT';
The included file include('../db.php'); please check the database connectivity has established or not?.

PHP: Delete || Confirm delete

I am having a problem.
I am creating a script that allows a person to select a record by it's primary ID and then delete the row by clicking a confirmation button.
This is the code with the form:
"confirmdelete.php"
<?php
include("dbinfo.php");
$sel_record = $_POST[sel_record];
//SQL statement to select info where the ID is the same as what was just passed in
$sql = "SELECT * FROM contacts WHERE id = '$sel_record'";
//execute SELECT statement to get the result
$result = mysql_query($sql, $db) or die (mysql_error());//search dat db
if (!$result){// if a problem
echo 'something has gone wrong!';
}
else{
//loop through and get dem records
while($record = mysql_fetch_array($result)){
//assign values of fields to var names
$id = $record['ID'];
$email = $record['email'];
$first = $record['first'];
$last = $record['last'];
$status = $record['status'];
$image = $record['image'];
$filename = "images/$image";
}
$pageTitle = "Delete a Monkey";
include('header.php');
echo <<<HERE
Are you sure you want to delete this record?<br/>
It will be permanently removed:</br>
<img src="$filename" />
<ul>
<li>ID: $id</li>
<li>Name: $first $last</li>
<li>E-mail: $email</li>
<li>Status: $status</li>
</ul>
<p><br/>
<form method="post" action="reallydelete.php">
<input type="hidden" name="id" value="$id">
<input type="submit" name="reallydelete" value="really truly delete"/>
<input type="button" name="cancel" value="cancel" onClick="location.href='index.php'" /></a>
</p></form>
HERE;
}//close else
//when button is clicked takes user back to index
?>
and here is the reallydelete.php code it calls upon
<?php
include ("dbinfo.php");
$id = $_POST[id];//get value from confirmdelete.php and assign to ID
$sql = "SELECT * FROM contacts WHERE id = '$id'";//where primary key is equal to $id (or what was passed in)
$result=mysql_query($sql) or die (mysql_error());
//get values from DB and display from db before deleting it
while ($row=mysql_fetch_array($result)){
$id = $row["id"];
$email = $row["email"];
$first= $row["first"];
$last = $row["last"];
$status = $row["status"];
include ("header.php");
//displays here
echo "<p>$id, $first, $last, $email, $status has been deleted permanently</p>";
}
$sql="DELETE FROM contacts WHERE id = '$id'";
//actually deletes
$result = mysql_query($sql) or die (mysql_error());
?>
The problem is that it never actually ends up going into the "while" loop
The connection is absolutely fine.
Any help would be much appreciated.
1: It should not be $_POST[id]; it should be $_POST['id'];
Try after changing this.
if it does not still work try a var_dump() to your results to see if it is returning any rows.
if it is empty or no rows than it is absolutely normal that it is not working.
and make sure id is reaching to your php page properly.
Ok as you are just starting, take care of these syntax, and later try switching to PDO or mysqli_* instead of mysql..
Two major syntax error in your code:
Parameters must be written in ''
E.g:
$_POST['id'] and not $_POST[id]
Secondly you must use the connecting dots for echoing variables:
E.g:
echo "Nane:".$nane; or echo $name; but not echo "Name: $name";
Similarly in mysql_query
E.g:
$sql = "SELECT * FROM table_name WHERE id="'.$id.'";
I hope you get it..take care of these stuff..

Sum values from rows in PHP

I'm trying to add values from row['scores'].
For example, if I have 6 rows that have a value of 1 for each row .. I want to be able to echo -> value of rows = 6.
The += is not working for me: I still get only the values themselves, e.g. 1,2,3,4,5,6,7 but I want the sum of it, let's say 1+2+3+4+5+6+7=28.
Thanks
<?php include("connect.php"); ?>
<html>
<head>
<title>Score Predictions</title>
</head>
<body>
<div id = "id">
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
}
?>
<?php
if (isset($_POST['submit'])) {
$x = $_POST["test"];
mysql_query("INSERT INTO test (home, away, score) VALUES ('$home', '$away', '$x')");
}
?>
<?php echo $home," - ",$away; ?>
<form method = 'post' action = 'http://albsocial.us/test/index.php'>
<select name = 'test'>
<option value = "" selected = 'selected'></option>
<option VALUE = '1'>1</option>
<option VALUE = 'X'>X</option>
<option VALUE = '2'>2</option>
</select>
<INPUT TYPE = 'submit' name = 'submit' />
</form>
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$score = $row['score'];
if ($score == "1") {
echo $sum += $score - 1;
}
}
?>
</div>
</body>
</html>
$sum=0;
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$score = $row['score'];
if ($score == "1"){
$sum = $sum+$score;
}
}
echo $sum;
try this.
it sume al $score values.
You have to remove the if condition and add the database value to $sum variable
$sum = 0;
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$score = $row['score'];
$sum += (int)$score;
}
echo $sum;
Several problems here, as other answers mostly fix, but to make clear:
your if $score == 1 doesn't seem relevant to your purpose. Did you mean if $id == 1, or were you just trying to ignore zeros? (anything + zero stays the same anyway, so you don't need to)
there doesn't seem to be a reason for subtracting one in $sum += $score-1 either
you need to finish the adding up first, and then call echo once. Currently, you have an echo for every database row, which is why you're seeing multiple numbers output.
if you're only displaying the sum anyway, you don't need to do this in a loop at all, just get the DB to add up for you, e.g. SELECT SUM(score) AS total_score FROM test or SELECT id, SUM(score) AS total_score FROM test GROUP BY id

Problem populating a dropdown box with MySQL query results (PHP/MySQL)

so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop

Categories