php and mysql only shoutbox - php

I am trying to build shoutbox using php and mysql only.
I am using the code below:
<?php
$sqldisplay =$Db1->query("select * from shoutbox ORDER BY date_time DESC");
?>
<h4>shout box input here</h4>
<form method="post" action="">
Message: <input type="text" id="message" name="message" class="message" />
<input type="submit" id="submit" value="Submit" name="shout" />
</form>
<?php
if(isset($_REQUEST['shout']))
{
$message = htmlspecialchars(mysql_real_escape_string($_POST['message']));
$sqlact =$Db1->query("Insert into shoutbox Values(NULL,NOW(),
'$username','$message')");
echo "save db";
}
?>
<table>
<thead><tr><td colspan="3">
<center>shout box output here</center></td></tr><tr><th>date</center></th>
<th><center>username</center></th>
<th><center>message</center></th></tr></thead>
<?php
while($row = mysql_fetch_array($sqldisplay))
{
echo "<tr> ";
echo "<td>" .$row[date_time] . "</td>";
echo "<td>" .$row[name] . "</td>";
echo "<td>" .$row[message] . "</td>";
}
echo "</tr> ";
?>
</table>
I know there are lots of jquery shoutbox available on net, but i have no idea about jquery. so posting my question here.
Problems:
1: I want the output to be displayed right after user press submit. his shout should also appear in the table with out refreshing the page
2:I want the output to displayed in scrolling manner as does the normal shoutbox shoul look like.e.g.
http://skrypty.klocus.pl/2012/01/php-ajax-shoutbox.html
Someone generous enough to help me in building this little script.

Related

html+php+database (very beginner)

I'd like to ask for help about my code. I'm still beginning to find way with html, php, databases and all that. This is something I already did some times, but somehow I got stuck at this point.
The connection is fine, so I omitted it.
The thing is that my buttons don't do anything when I click them, it's like there's no action to be taken. At this point I can't notice my mistake, but I'm sure it's very simple. :(
I've translated some things from the code from my native language to English, so if you find it inconsistent in that regard, I'm sorry. *
I'd guess it's something I've messed up on the table structure
Thank you!
<?php
echo "<th>City |</th>";
echo "<th>Update |</th>";
echo "<th>Delete |</th>";
echo "</tr>";
$query = "SELECT name, id_city as id FROM city";
$result = pg_query($conn, $query);
if($result) {
while($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo '<form method="post" action="./updatecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Update">';
echo '</form>';
echo '</td>';
echo '<td>';
echo '<form method="post" action="./deletecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tr>';
}
}
pg_close($conn);
?>
Your approach to making form is completely wrong, what you are doing in simply echoing texts, No form is getting generated. Hence the button is not working because there is no button.
What you need to do it something like this :
<?php
#Write your php related code here
#Like connecting to database
?>
#All your html related content goes here like making tables
<form method="post" action="./updatecity.php">
<input type="hidden" name=id_city value="<?php echo $row['id'] ?>">
<input type="submit" name="submit" value="Update">
</form>
<?php
//Write your php related code here
?>

How do I create an html form with multiple checkboxes

This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value

Need assistance displaying current mysql record and modifying in php form

Thanks in advance for any light shed.
I have a mysql database consisting of customers with some fields pertaining to each customer. currently running on one of my lamp servers. There is security risks with my code at the moment, but I plan to get the functionality i'm looking for and then reconfigure the code for a tighter security. At the moment I have an html index file that calls on php script to search mysql database by firstname or lastname. Upon this query it displays a list of users and allows me to modify the user. When I click modify it pulls the correct customer id number, but it is not displaying any current information, nor allowing me to update the info.
To summarize, I would like to search a customer, and it pull up selected fields and show the content and allow me to actively change the data and resend it to the database.
My search.html code:
<html>
<body>
<form action="scripts/search.php" method="post">
Firstname: <input type="text" name="firstname">
<input type="submit">
</form>
<form action="scripts/lastnamesearch.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>
<form action="scripts/phonenumbersearch.php" method="post">
Phone Number: <input type="text" name="phone">
<input type="submit">
</form>
</body>
</html>
MY search.PHP Script:
//this script allows me to search the database by filling out one of the forms and clicking submit. Each of the forms calls upon it's own individual script, I realize that this is probably cumbersome, due to my lack of coding knowledge.
<?php
$con=mysqli_connect("localhost","root","*****","*******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers WHERE `firstname` LIKE '$_POST[firstname]'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>phone</th>
<th>address</th>
<th>notes</th>
<th>additional notes</th>
<th>passwords</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['addnotes'] . "</td>";
echo "<td>" . $row['passwords'] . "</td>";
echo "Modify User";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My modify.php script:
//this is where I believe one of my problems lie. when I click modify user on the search.php script it calls on this script and it loads the correct user/customer id in the address bar, but it doesn't show any existing data, nor does it update the data that I fill in the cells.
<?php
$con=mysqli_connect("localhost","root","crapola1","Computition");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mysqli_query = "SELECT * FROM customers WHERE ID = $_get[id]";
$mysqli_result = mysqli_query($mysqli_query);
$customer = mysqli_fetch_array($mysqli_result);
?>
<h1> You are modifying a user</h1>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Firstname<input type="text" name="inputFirstname" value="<?php echo $row['firstname']; ?>" /><br />
Notes<input type="text" name="inputNotes" value="<?php echo $row['notes']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify" />
</form>
Thanks again,
I've been searching on this topic for about a week now and have pieced together this much, but can't seem to get over this "hump"
$_GET is a super global array . It should be in UPPERCASE.
Change the query on your modify.php here
SELECT * FROM customers WHERE ID = $_get[id] to upper case.
Must be..
SELECT * FROM customers WHERE ID = ".$_GET['id']
Also, It is strictly not advised to pass the $_GET or $_POST parameters directly to your query as it leads to SQL injection. You need to switch over to PreparedStatements

How to display results from search below the form

i would like to ask if someone could help me with a search query and displaying of the results.
Here is the code...
<?php
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<table border=1>";
echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
echo "</table>";
}
echo "</ol>";
mysql_close();
?>
In content i added the form ...
<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
So ... all is working normaly.. i am getting the results, everything is fine. Problem is:
i want to have results displayed below the form itself, not in a new page.
If anyone could help me that would be great. Thank you in advance
P.S.
Well i have no idea how it works actualy but was thinking, isnt there a way where the result can be added into empty div below the form or something like this? I tryed the options above but it dosnt helped.
Save your table in to a variable:
$table = "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
$table .= "<table border=1>";
$table .= "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
$table .= "</table>";
}
$table .= "</ol>";
And print it in your template:
<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
<?php echo $table ?>
Just embed your form code in your search.php and then check for isset($submit) and you are good to go.
<?php
?>
<form action="" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
<?php
if(isset($submit))
{
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_POST['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<table border=1>";
echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
echo "</table>";
}
echo "</ol>";
mysql_close();
}
?>
You've got two choices,
Use AJAX to actually get the results to 'collect' the PHP Generated Content and use JavaScript to append it to somewhere on the page.
Place the Search Algorithm and PHP Code at the top of the same page of the form, then use an isset() $_GET or $_POST to check if it's been submitted successfully, save the results & content to be later printed elsewhere.

Delete Row with Input Button

I am trying to create a table that will allow me to delete a row by clicking a delete button. I did find an article on here at the following link that did provide some help but I am still unable to get it to work: Show all rows in mysql table then give option to delete specific ones .I have written most of the code but I am having problem with line 10 on the delete.php page. Ideas?
Main.php
$result = mysqli_query($con,"SELECT * FROM cs_general_info");
echo "<table><tr><td>Delete</td><td>First Name</td><td>Last Name</td><td>Address</td>td>Phone</td><td>E-Mail Address</td></tr>";
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td>
<form action="delete.php" method="POST">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
<input type="Submit" value="Delete"></form>
</td>
<?php
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
Delete.php
"DELETE FROM cs_general_info WHERE id=".mysql_real_escape_string($delete_id);
Hopefully delete.php is more than that single line. In delete.php you will want to catch the POST method and the $row[id]; bind it, then delete it.
To get the id of the row you want to delete in your delete.php file, you need to get the variable, like this:
$delete_id = $_POST['delete'];
WARNING: This is the very basic functionality you need to get this to work. However, you will need to implement many other levels of security before this should ever go into any production code.
If that is your delete.php, then you are missing a lot. You need a database connection and you need to execute the SQL command against the database. But that is the least of your worries until you get your parameters via $_POST.
Here is the PDO Manual for the database connection and SQL execution.
And here is the manual for $_POST.
You want to either put a link or inline form within each of your table’s rows, and then have a script that deletes the specified row and returns the user to your page.
<table>
<tbody>
<?php foreach ($results as $result): ?>
<tr>
<td><?php echo $result->title; ?></td>
<td>
<form action="delete.php" method="post">
<input type="hidden" name="id" value="<?php echo $result->id; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And then delete.php:
<?php
if (isset($_POST['id']) && intval($_POST['id']) > 0) {
// create PDO instance; assign it to $db variable
$sql = "DELETE FROM `table` WHERE `id` = :id LIMIT 1";
$smt = $db->prepare($sql);
$smt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$smt->execute();
}
header('Location: index.php');

Categories