How to use explode() with php variables [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm using a html table where each row and column has his own ID (e.g. name="23_3").
But this ID is made of 2 variables $calUser= $x['idTask'] . "-" . $y['idTime'];
This is how I explode() it:
list($uTask,$uTime) = explode("-", $_POST['$calUser'], 2);
But it's not working any ideas?
EDIT
Sorry forgot the input
<input type='text' name='" . $calUser . "' value='$calUser'>

Input in your case should be:
<input type='text' name='calUser' value='". $calUser . "'>
Validate this by taking a look at the produced page's soure code.
After that you can get the content by calling:
list($uTask,$uTime) = explode("-", $_POST['calUser'], 2);
But as with all user provided content ($_POST), check if the content really is within the range of expected values to prevent XSS and injection attacks on your page.

Fist of all, you are trying to explode $calUser, or the value corresponding to the $calUser key?.
I think you should do:
list($uTask,$uTime) = explode("-", $_POST[$calUser], 2);
Remove the quotes on the $_POST key, or you will be asking for the literal '$calUser'
EDIT
Also, your input tag must be:
<input type='text' name='" . $calUser . "' value='" . $calUser ."'>
Altough i dont know if rou are using a string for the tag, or directly echoing to ouput, so some like:
<?php
echo "<input type='text' name='" . $calUser . "' value='" . $calUser ."'>";
Would be advised, and my favorite syntax:
<?php
echo "<input type='text' name='{$calUser}' value='{$calUser}'>";
I like the brackets, as are more friendly even with SQL...

Related

Php Why does mysql query adds unnecessary extra html tags? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
In php i have a SQL query that takes column name and id from table called subjects and will show them on my webpage.
For some reason i am getting extra < a > tag at the end of the first < a > tag.
Any help?
function subjects($id, $name){
return '<li>' . '<a class="subject-link" href="threads.php?thread=' . $id . '">' . $name . '<a/>' . '</li>';
}
$stmt = $pdo->query('SELECT name, id FROM subjects ORDER BY id ASC');
$allFileNames = $stmt->fetchAll();
$x = subjects($allFileNames[0]['id'], $allFileNames[0]['name']);
echo $x;
THIS IS WHAT I GET IN HTML:
<li><a class="subject-link" href="threads.php?thread=12">loodus</a><a></a></li>
As you can see there are some unnecessary extra < a > tags at the end of the list.
THIS IS WHAT I WANT IN HTML:
<li><a class="subject-link" href="threads.php?thread=12">loodus</a></li>
Notice your code...
$name . '<a/>' . '</li>';
that should be </a>.

How do I show only the column name of the SQL table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Currently my code consists of
$sql = "SHOW columns FROM tblexercise";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
This allows me to show the column names but it also includes all the attributes and types etc.
is there any way to show just the column name?
You don't need a loop inside a loop. You only need one foreach loop and then you can access the key Field which holds the name of the column. The query SHOW COLUMNS is explained in the MySQL doc. You can check in that link what are the results of this query and their sample values. Then you can decide which values you want to access.
$result = $conn->query("SHOW columns FROM tblexercise");
foreach ($result as $row) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "</tr>";
}

Data is not showing after form submited using POST [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a form using a POST method, upon enter a name and clicking search, it is supposed to display the information from the database, with the term you used.
index.php
<form action="search.php" method="POST">
<input type="text" name="search" /><br/><br/>
<input type="Submit" value="Search" />
</form>
Search.php
<?php
if (!$_POST) {
include('index.php');
} else {
?>
<h1>Server name<br />Official ItemDB!</h1>
<br />
<?php
$search = $_POST["search"];
MySQL_connect("localhost", "pernix_items", "#");
MySQL_select_db("pernix_items");
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'");
while($row = mysql_fetch_array($result));
{
?>
<table border="1">
<?
echo '<tr>';
echo '<td>'.$row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['desc'] . '</td>';
echo '</tr>';
}
}
?>
</tr>
</table>
I added the mysql_error(); function to dertime where I was going wrong, to this
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'" or die(mysql_error()));
and gives me this
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/pernix/public_html/tools/item-list/search.php on line 21
My line 21 of search.php
while($row = mysql_fetch_array($result));
So I added another or die print error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
Any ideas?
Although resolved in the comments, reposting it here as a whole answer.
First the or die() line is wrong, as the die statement is executed inside the mysql_query and not as supposed to when and if the query fails, as it should be:
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'") or die(mysql_error());
Secondly you have an extra semicolon right after the while() line making, which should be removed to look like:
while($row = mysql_fetch_array($result))
{}
Last and most importantly you should really convert this code to mysqli or pdo as the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future.

assign value to checkbox using loop php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The $file-name should be for example: a.pdf,b.pdf,c.pdf,d.pdf respectively.
But why I cannot assign the value into the checkbox?
please help/__\
while($row = Mysqli_fetch_array($result))
{
$file-name = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]'value=<?php echo $filename ?>' > </td>";
}
Per the comments above, try the following:
while($row = Mysqli_fetch_assoc($result))
{
$filename = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]' value='".$filename."' /> </td>";
}
As the comments to your question mentioned you were assigning $file-name (an invalid variable name in PHP) and trying to use a (presumably) unassigned variable: $filename you also were missing an opening single-quote for the value attribute which could prevent it from properly rendering, and you can use string concatenation instead of trying to echo-inside-an-echo.

PHP If statement doesn't append variable properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am querying my database and dumping the data into an HTML table. One of my database keys is called "time_expire", with a value of -1 meaning never. Therefore, to show something to the effect of "never" in the HTML table instead of the raw data I am attempting to change the variable before it is echo'd.
This is my code
<?php
$sql = "SELECT * FROM penalties ORDER BY id DESC";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
if ($rows['time_expire'] = '-1') {
$rows['time_expire'] = '<span class="label label-important">Permanent</span>';
}
echo "<tr>";
echo '<td>' . $rows['id'] . '</td>';
echo "<td>" . $rows['client_id'] . "</td>";
echo "<td>" . $rows['type'] . "</td>";
echo "<td>" . date('m/d/Y', $rows['time_add']) . "</td>";
echo "<td>" . $rows['duration'] . "</td>";
echo "<td>" . $rows['time_expire'] . "</td>";
echo "<td>" . $rows['reason'] . "</td>";
echo "</tr>";
}
?>
This code does not error, however every row in my HTML table has an expiration value of "never", even if the the raw data is different.
This seems to be a common typo in the if statemenet
if ($rows['time_expire'] = '-1') [
^
|
If you want to the interpreter spot these for you, try using yoda conditions like this:
if ('-1' = $rows['time_expire'])
This form will create error however the correct forms both work ok:
'-1' == $rows['time_expire']
or
$rows['time_expire'] == '-1'
I think it is supposed to be:
if ($rows['time_expire'] == '-1')
not
if ($rows['time_expire'] = '-1')
First what type is time_expire?
If time_expire is numeric then you should change the condition to:
if ($rows['time_expire'] == -1) which will evaluate properly.
Secondly your condition checking is actually assigning the value to $rows['time_expire'] with single =, it should be double = like ==.
Try changing If condition to if ($rows['time_expire'] == '-1'). Right now what your condition says that variable $rows['time_expire'] is equal to '-1' which is always returning true.It might get you in the unlimited looping.

Categories