Change Color for checkin form - php

I want to create a form that displays records from my database
name, email, phone number and situation , and after selecting I want to change the background color of the situtaton itdepends the value ?
<form method="post">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ID = isset($_POST['ID']) ? $_POST['ID'] : false;
$connect = mysql_connect('localhost', 'root', '','seminar');
mysql_select_db ('seminar');
$sql = "SELECT `name`, `del_company`, `phone`, `email`, ,`situation` FROM `dele` WHERE ID = $ID";
$res = mysql_query($sql);
if(! $ID ) {
die("Could not get data:" . mysql_error());
} while($row = mysql_fetch_array($res)) {
while($row = mysql_fetch_array($res)) {
echo "<br><p><b>Surname: </b></b></b>", $row['name'], "</p>";
echo "<br><p><b>company: </b></b>", $row['del_company'], "</p>";
echo "<br><p><b>phone: </b></b></b></b>", $row['phone'], "</p>";
echo "<br><p><b>email: </b></b></b></b></b>", $row['email'], "</p>";
echo "<br><p><b>expert: </b><br>", $row['situation'], "</p>";
}
}
else {
echo "<p>Enter a valid ID above</p>";
}
mysql_close($connect);
?>

Change this line
echo "<br><p><b>expert: </b><br>", $row['situation'], "</p>";
to something like this:
echo "<br><p class=".$cssClass."><b>expert: </b><br>", $row['situation'], "</p>";
Before all this you need to create some logic that says if situation is whatever, then $cssClass = whatever. Then add in some CSS for each of the classes.
Something like this:
if ($row['situation'] == "horse") {$cssClass == "blue-color"};
elseif ($row['situation'] == "cow") {$cssClass == "red-color"};
else {$cssClass == "yellow-color"};
Then just define your CSS:
.blue-color{
background-color:blue;
}
ETC.

Related

How to pass posted variables to an Anchor ID tag in PHP

I have been trying to insert some data into a table in DB, but the error came at where one of my fields is GET variable from the anchor tag.
To get a clear understanding please take a look at my code:
index page: with a post value of id = 1
<form name="user_form" method="post" action="input.php">
<?php
$sql2 = "SELECT * FROM singleq ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$id = $row2['id'];
$cat = $row2['cat'];
?>
<tr >
<input type="hidden" name="q_id" id="q_id" value="<?php echo ($row2["id"]); ?>" />
<input type="hidden" name="tablum" id="tablum" value="singleq" />
<input type="hidden" name="test_id" id="test_id" value="<?php echo $_GET['id'] ?>" />
<td><a href='singleqbanksub.php?id=<?php echo $row2['id'];?>'><?php echo substr(($row2["question"]),0,100) ?>...</a></td>
<td><button class="button button1" type="submit" name="insert-data" id="insert-data" style="width:180px;">Add Question</button></td>
</tr>
<?php }
} else {
echo "";
}
?>
</form>
input.php :
<?php include('db.php'); ?>
<?php
$q_id = $_POST['q_id'];
$test_id = $_POST['test_id'];
$tablum = $_POST['tablum'];
$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO english (cat, test_id, q_id, tablum)
VALUES ('$cat', '$test_id', '$q_id', '$tablum')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php?id='echo "$test_id";'');
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here as I tried to pass the id through the anchor tag redirection to the same page to loop the process, it shows an error about an unidentified echo.
You are using echo inside header function which is wrong , use like this:
header('Location: index.php?id='.$test_id);

MYSQL and PHP Loop Failure

What problem i am having right now, is that there is a while loop to post my topics retrieved from MySQL. Works perfectly fine, until I get into inputting data. I have recently created a comment system, where for each topic there will be a comment box to submit. The problem is the while loop runs it over and over again, so when i type a comment for one topic, it posts to all of them.
Here is my code:
//MYSQLI LOGIN DETAILS
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
//MYSQLI CREATE CONNECTION
$constatus = new mysqli($servername, $username, $password, $dbname);
//MYSQLI CHECK CONNECTION
if ($constatus->connect_error) {
die("Connection failed: " . $constatus->connect_error);
}
//MYSQLI COUNT COLUMNS
$sql = "SELECT NEWSID, AUTHOR, ADMINSTS, DATE, HEADING, ARTICLE FROM news ORDER BY NEWSID DESC";
$result = $constatus->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class=newsboard_topic>" .
"<div class=newsboard_authordate>" . $row["AUTHOR"];
if ($row["ADMINSTS"] == admin) {
echo
"<div class=newsboard_adminfx>
Admin
</div>";
} else if ($row["ADMINSTS"] == sadmin) {
echo
"<div class=newsboard_sadminfx>
Super Admin
</div>";
}
if ($_SESSION['adminsts'] == 'admin' || $_SESSION['adminsts'] == 'sadmin') {
echo "<span class=newsboard_adminactions> <img src='/image/remove.png' style='width:20px; height:20px;'> </span>";
}
echo
"<span class=date>" . $row["DATE"] .
"</span></div>
<h1>" . $row["HEADING"].
"</h1><p class=newsboard_topic_article>" .
$row["ARTICLE"] .
"</p>";
$sqlcomments = "SELECT newscomments.USERID, newscomments.COMMENT, userdata.FIRSTNAME, userdata.LASTNAME, userdata.ADMINSTATUS FROM newscomments JOIN userdata ON newscomments.USERID=userdata.ID WHERE NEWSID=$row[NEWSID] ORDER BY COMMENTID DESC";
$resultcomments = $constatus->query($sqlcomments);
echo "<div class=newsboard_comments>
Comments
<br>";
while($rowcomments = $resultcomments->fetch_assoc()) {
echo $rowcomments["FIRSTNAME"] . " " . $rowcomments["LASTNAME"] . " " . $rowcomments["COMMENT"] . "<br>";
}
if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
echo '
<form method="post">
<input class=postheadline type="text" name="comment" />
<input class=submit type="submit" id="submit" name="submit" value="Comment"/>
</form>';
if (isset($_POST[submit])) {
if (!empty($_POST[comment])) {
$sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$row[NEWSID]', '$_SESSION[profileid]', '$_POST[comment]')";
if ($constatus->query($sqlcommentpost) === TRUE) {
echo "Posted Successfully!";
break;
} else {
echo "Fatal Error. Please try again";
break;
}
}
}
}
echo "</div></div>"; /*Ends newsboard_topic Div & newsboard_comments Div*/
}
} else {
echo "0 results";
}
Live example is online at www.geovillageva.com however you cannot see comments as it is for registered members only because it will have a name for posters.
Lets include the news id also in the form. You can have a hidden input field for this. Then use this news id $_POST[nid] while inserting.
if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
echo '
<form method="post">
<input class=postheadline type="text" name="comment" />
<input type="hidden" name="nid" value="'.$row[NEWSID].'" />
<input class=submit type="submit" id="submit" name="submit" value="Comment"/>
</form>';
if (isset($_POST[submit])) {
if (!empty($_POST[comment])) {
$sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$_POST[nid]', '$_SESSION[profileid]', '$_POST[comment]')";
if ($constatus->query($sqlcommentpost) === TRUE) {
echo "Posted Successfully!";
break;
} else {
echo "Fatal Error. Please try again";
break;
}
}
}
}
So, your input block
if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
echo '
<form method="post">
<input class=postheadline type="text" name="comment" />
<input class=submit type="submit" id="submit" name="submit" value="Comment"/>
</form>';
if (isset($_POST[submit])) {
if (!empty($_POST[comment])) {
$sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$row[NEWSID]', '$_SESSION[profileid]', '$_POST[comment]')";
if ($constatus->query($sqlcommentpost) === TRUE) {
echo "Posted Successfully!";
break;
} else {
echo "Fatal Error. Please try again";
break;
}
}
}
}
needs to go by itself before the display loop. On the query inserting using $row[NEWSID], you need to use $_POST['newsid'] instead (and you may need to add that to your form to be posted along with the comments).
Please note that you need to beef up the security on this considerably or you will be hacked.
You can try a .reset() on your form after the submission was successful(before the break).

Image not showing (broken image) but search works

i'm new in programming and have been working on a searchable database which can retrieve images by typing in keywords and after pressing submit will show results and the picture.
But so far i have no luck in getting the picture to show(broken link/image) but my search form does work and does correctly retrieve the name or result.
My table in phpmyadmin name is shoes , and i have 3 columns, 1 id (int15 PRI) ,2 brand/model (varchar 50), 3 picture (longblob).
My code is relative simple and hope you can help me out =)
File name: search.php
<form action="search.php" method="POST">
Name: <input type ="text" name="search_name"> <input type="submit" value="Search">
<?php
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)){
if (strlen($search_name)>=3) {
$query = "SELECT * FROM `shoes` WHERE `brand/model` LIKE '%".mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows>=1) {
echo $query_num_rows.' Results found:<br>';
while ($query_row = mysql_fetch_array($query_run)) {
$picture = $query_row['picture'];
echo "</br>";
echo $query_row ['brand/model'];
echo "</br>";
echo "</br>";
//header("content-type: image/jpeg");
echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";
echo "</br>";
}
} else {
echo 'No Results Found.';
}
} else {
echo 'Text field must be more than 3 characters.';
}
} else {
echo 'Text Field Cannot be Empty!';
}
}
?>
i have a image.php here
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("phsdatabase");
if(!$db){
echo mysql_error();
}
$id = $_GET['id'];
$query = "SELECT `picture` FROM shoes where id='$id'";
$query_run = mysql_query("$query",$conn);
if($query_run){
$row = mysql_fetch_array($query_run);
$type = "Content-type: image/jpeg";
header($type);
echo $row['picture'];
} else {
echo mysql_error();
}
?>
storeinfo.php to store new info,
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("phsdatabase",$conn);
if(!$db)
{
echo mysql_error();
}
#$brandmodel = $_POST['brand/model'];
#$picture = addslashes (file_get_contents($_FILES['picture']['tmp_name']));
#$image = getimagesize($_FILES['picture']['tmp_name']);//to know about image type etc
//$imgtype = $image['mime'];
if (isset($_POST['brand/model'])){
$brandmodelentry = $_POST['brand/model'];
if (!empty($brandmodelentry)){
if (strlen($brandmodelentry)>=3) {
$query ="INSERT INTO shoes VALUES('','$brandmodel','$picture')";
$query_run = mysql_query($query,$conn);
echo '<br>';
echo "Information Stored Successfully!";
} else {
echo mysql_error();
}
echo '<br>';
echo '<br>';
echo "Thank you for Registering new information to our database!";
} else{
echo 'Text Field cannot be empty!';
}
}
?>
newentry.php which register new info
<form enctype="multipart/form-data" action="storeinfo.php" method="POST">
<center>Shoes Information</center>
Brand and Model Name<input type=text name="brand/model">
Picture of Shoes(Acceptable formats:<br>JPEG,JPG,PNG)<input type="file" name="picture" id ="picture">
<input type=submit name="submit" value="Store Information">
Your code is absolutely correct except single line i.e.
echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";
You have to change the line to :
echo '<img src="data:image/jpeg;base64,'
.base64_encode($image['file_data']).'" width=300 height=200/>";
In my experience, the problem my image was broken when I tried to display it from database is the the length of the image, I mean from the database where you put the length of a varchar you should change it to long text.
Your image source should be image file extension not php extension, Please check :
echo "<img src='any jpg,png or gif exetension path' width='300' height='200' />";
for example:
echo "<img src='imagename.png' width='300' height='200' />";

Echo inside and Echo

I'm using echo to sum up the results form a Table. Inside this echo, I place another echo to show all the results form another table. On this page I have an array, I want every item in that array, that is also in the result from the table, to be checked. I use the code below (remember: this code is inside another echo!), it's not functioning, why not?
<?php
$query = "SELECT * FROM profilestemp";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$merkenarray = unserialize($row[merken]);
echo "
A VERY BIG OTHER PART OF THE FORM....
<tr>
<td style=\width:150px;background-color:#a8c11f;padding:2px;\"><p style=\"color:White;font-weight:bold\"><b>Merken</b></p></td>
<td><div id=\"rubrieken\">
<?php
$sql = \"SELECT merknaam FROM merken\";
$result = mysql_query($sql);
while ($row2 = mysql_fetch_array($result)) {
if (isset($merkenarray) && is_array($merkenarray) && in_array($row2[merknaam], $merkenarray)) {
$checked = \"checked='checked'\";
}
else $checked = \"\";
echo \" <input \".$checked.\" type=\"checkbox\" name=\"merken[]\" value='\" . $row2[merknaam] . \"'> \" . $row2[merknaam] . \" <Br /> \";
}
?>
</div></td>
</tr>
}
?>
Instead of using echo in your function, have it return the string to be output. An example:
function functionName() {
return 'Some content to be output';
}
echo functionName();
Additionally, this will give your function more flexibility, as you might not want to echo the result every time, e.g:
function functionName() {
return 'Some content to be output';
}
// Write the result of functionName to a file
file_put_contents('content.txt', functionName());
Your code should look like this
<?php
$query = "SELECT * FROM profilestemp";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$merkenarray = unserialize($row[merken]);
?>
A VERY BIG OTHER PART OF THE FORM....
<tr>
<td style="width:150px;background-color:#a8c11f;padding:2px;"><p style="color:White;font-weight:bold"><b>Merken</b></p></td>
<td><div id="rubrieken">
<?php
$sql = "SELECT merknaam FROM merken";
$result = mysql_query($sql);
while ($row2 = mysql_fetch_array($result)) {
if (isset($merkenarray) && is_array($merkenarray) && in_array($row2[merknaam], $merkenarray)) {
$checked = "checked='checked'";
}
else $checked = "";
echo " <input ".$checked." type="checkbox" name="merken[]" value='" . $row2[merknaam] . "'> " . $row2[merknaam] . " <Br /> ";
}
?>
</div></td>
</tr>
<?php }?>

How to make a dropdown with data from a mysql table?

I wanted to make a dropdown area by getting the values from a mysql table. This code does not seem to work; all it prints out is the box for the dropdown without the content, how can I get this to work? or is there an alternative procedure in doing this?
<?
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test", $connection);
$query = "SELECT full_name FROM test";
$names = mysql_query($query);
function dropDown($content, $result)
{
while($row = mysql_fetch_row($result))
{
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
}
$content=<<<EOT
<html>
<body>
<select name="name">
EOT;
dropDown($content, $names)
$content.=<<<EOT
</select>
</body>
</html>
EOT;
echo $content;
?>
return the string. PHP is not C where you use out parameters just because they are sometimes handy.
function dropDown($result, $fieldName)
{
$content = '';
while($row = mysql_fetch_row($result)) {
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
return '<select name="'.$fieldName.'">'.$content.'</select>';
}
$content = '<html><body>';
$content .= dropDown($names, 'name');
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly
or simply the code is
<?
$sql = "SELECT * FROM userregistraton ";
$result = mysql_query($sql); ?>
<select name="username" id="username">
<option value="">Select user</option>
<?php
while ($row = mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row['uid']?>" <?php if($row['uid']==$_POST["username"]) echo "selected"; ?> >
<?php echo $row['username'];?></option>
<?php
} ?>
<?php echo $form->dropDownList($model,'courseprefer', array(
'(*value in database table*'=>'displaying value',
)); ?>
you can manually add them just make sure that the first value in array is in the database table.. :)
to avoid error!

Categories