I have a simple dropdown menu which posts the result to itself but when i choose one of the options in the drop down menu it does not echo back the result as expected.
I'm sure i've just missed out something simple but can't spot it. Any ideas? The form posts but does not echo back $user_settings.
<?php
include "functions.php";
connect();
$sql="SELECT user_id, user_realname FROM users ORDER BY user_realname ASC";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$name=$row['user_realname'];
$options.="<OPTION VALUE=>".$name.'</option>';
}
if(isset($_POST['submit'])){
$user_realname = $_POST['username_select'];
$user_select = mysql_query("SELECT user_id, user_realname FROM users WHERE user_realname = '$user_realname'")
or die ("Could not get user data");
while($row = mysql_fetch_array($user_select)){
$user_settings = $row['user_id'];
echo $user_settings;
}
}
?>
<html>
<head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<tr><label>Choose User to Edit</tr>
<tr><SELECT NAME="username_select"><OPTION VALUE=""></option>User's Name<?php echo $options;?></SELECT></label></tr>
<tr><input type="submit" value="submit" name="submit"></tr>
</form>
<?php echo $user_settings;?>
<br/>
Go Back
</body>
</head>
</html>
User $_SERVER['PHP_SELF'] instead of $PHP_SELF
Related
I'm starting with php (but I was quite experienced on C++) and I have some questions. The thing is that I would like to have several forms in the index.php, and to have the code more clean I would like to call functions to call the forms.
Right now, I'm having the running code as teh follow:
<html>
<head>
<title>Prueba de PHP</title>
</head>
<body>
<form id="nomfromnum" action="getCharName.php" method="GET">
<input type="number" min="1" name="charidbox" required>
<input type="submit" value="Seleccionar personaje">
</form>
<form id="despPers" method="POST">
<select name="nomChar">
<?php
include("conection.php");
$con=conectar();
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
?>
</select><br>
</form>
</body>
</html>
In the first form, I was able to move it into another file (getCharName.php). But for the second function (despPers), that creates a dropdown menu and populates it with its values, I wan't able to find the way to move it to another file. I tried to chance the header for and create a file despPers.php with the following code but it didn't worked.
<select name="nomChar">
<?php
function despChar($conexion){
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
}
include("conection.php");
$con=conectar();
despChar($con);
$con->close();
?>
</select><br>
Can anybody guide me with this? Maybe it's something basic, but I'm quite freshman on php and html.
Thanks a lot!
Oops, it was a wrong variable name.
Anyway, here you have what I have done.
On the index.php
<form id="despPers" action="despChar.php" method="POST">
<select name="nomChar">
<?php
include("despChar.php");
despChar();
?>
</select><br>
</form>
And the despChar.php is
<?php
function despChar(){
include("conection.php");
echo '<option value="0">Character</option>';
$con=conectar();
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
$con->close();
}
?>
This question already has answers here:
Update a MySQL database field data with HTML form and PHP
(2 answers)
Closed 5 years ago.
So I'm currently playing around with PHP and making a "blog" system. I want to user to be able to edit the topic name of their own posts. Currently when you edit a topic name, all of the other topic names changes no matter which user made the post.
Only edit the current topic name.
Only the editor who made the post can edit that post.
topic.php
<?php
session_start();
require('connect.php');
if (#$_SESSION["username"]) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
</head>
<body>
<?php include('header.php'); ?>
<center>
<?php
if (#$_GET['id']) {
$check = mysql_query("SELECT * FROM topics WHERE topic_id='".$_GET['id']."'");
if (mysql_num_rows($check) > 0) {
while ($row = mysql_fetch_assoc($check)) {
$check_u = mysql_query("SELECT * FROM users WHERE username='".$row['topic_creator']."'");
while ($row_u = mysql_fetch_assoc($check_u)) {
$user_id = $row_u['id'];
}
echo "<h1>".$row['topic_name']."</h1>";
echo "<h5>By <a href='profile.php?id=$user_id'>".$row['topic_creator']."</a><br />Date: ".$row['date']."</h5>";
echo "<br />".$row['topic_content'];
echo "<br /><br /><img src='img/".$row['image']."' width='300' />";
echo "<br /><br /><a href='edit.php?edit=".$row['topic_id']."'>Edit</a>";
}
}else {
echo "Topic not found.";
}
}
?>
</center>
</body>
</html>
<?php
}else {
echo "You must be logged in.";
}
?>
edit.php
<?php
session_start();
require('connect.php');
if (#$_SESSION["username"]) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
</head>
<body>
<?php include('header.php'); ?>
<center>
<?php
if( isset($_GET['edit']) )
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM topics");
$row= mysql_fetch_assoc($res);
}
if( isset($_POST['newTn']) )
{
$newTn = $_POST['newTn'];
// $id = $_POST['id'];
$sql = "UPDATE topics SET topic_name='$newTn'";
$res = mysql_query($sql)
or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
<form action="edit.php" method="POST">
Name: <input type="text" name="newTn" value=<?php echo $row['topic_name']; ?>><br />
<input type="hidden" name="id" value="">
<input type="submit" value=" Update "/>
</form>
</center>
</body>
</html>
<?php
if (#$_GET['action'] == "logout") {
session_destroy();
header("Location: login.php");
}
}else {
echo "You must be logged in.";
}
?>
Thanks beforehand!
//E
In edit.php
You need to specify the post id to be edited in the query.
if( isset($_POST['newTn']) )
{
$newTn = $_POST['newTn'];
$id = $_POST['id'];
//notice here the $id is added as where clause to filter the edit on one row only
$sql = "UPDATE topics SET topic_name='$newTn' WHERE post_id = '$id'";
$res = mysql_query($sql)
or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
$sql = "UPDATE topics SET topic_name='$newTn' where topic_id = '".$_GET['edit]."'";
You have passed the topic id from Grid and you need to attach that in query
You need to specifies the id of the topic in the query UPDATE :
$sql = "UPDATE topics SET topic_name='$newTn' where id=$session[yourtopicID]" ;
In edit.php change UPDATE topics SET topic_name='$newTn' query to below
UPDATE topics SET topic_name = '$newTn' where `yourTopicId` = '$_GET[edit]'
update
Can anyone explain to me why I am getting duplicate messages instead of one?
how can I change my code so that when I type a comment and press "Comment" button, it will only display one message instead of duplicates! When I have one comment boxes it doesn't show duplicate comments, but if I have more than one then it starts duplicating!
COMMENT.INC.PHP
include 'cdbh.inc.php';
function setComments($con)
{
if (isset($_POST['commentSubmit'])) {
$uid = mysqli_real_escape_string($con,$_POST['uid']);
$date = mysqli_real_escape_string($con,$_POST['date']);
$message = mysqli_real_escape_string($con,$_POST['message']);
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid','$date','$message')";
$result = mysqli_query($con,$sql);
}
}
function getComments($con)
{
$sql = "SELECT * FROM comments";
$result = mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc($result)) {
echo $row['uid'];
echo ":";
echo $row['message']."<br><br>";
}
}
page code
<?php
date_default_timezone_set('America/Los_Angeles');
include 'comment.inc.php';
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="comment.css" rel ="stylesheet">
</head>
<body>
<?php
$sql="Select * from tbl_images";
$result=mysqli_query($connection,$sql);
while ($row=mysqli_fetch_array($result)) {
?>
<img src="images/<?php echo $row['images_name'] ?>" width="200px" height="200px">
<?php
echo "<form method ='POST' action ='".setComments($con)."'>
<input type ='hidden' name ='uid' value='unknown'>
<input type ='hidden' name ='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea>
<button type ='submit' name='commentSubmit'>Comment</button>
</form>";
}
getComments($con);
?>
</body>
</html>
Maybe you are submiting all your forms instead of one..
check your database in order to know from what img comes each message.
If you have other code like javascript, you should post it.
I want to print the name and last name of an ID entered in the text box. Here is the PHP and HTML code:
<head>
<title>
Search your name by ID
</title>
</head>
<?php
if(isset($_POST["searchname"]))
{
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
$a = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($a);
echo "$row[0] , $row[1] , $row[2]";
}
else
{
echo "error";
}
?>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
Output when I enter ID:
, ,
There are entries in the MySQL table but I am unable to fetch them. What is wrong with my code?
UPDATE: I have also tried mysql_fetch_array but it is not working.
Main problem is that you're miximg mysqli and mysql. These are absolutely different APIs.
Assuming you have
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
Next you should:
$result = $connect->query("Select * from users where id='$id'");
Then get results:
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
And of course, instead of directly putting values into your query use prepared statements.
Update:
about mistakes:
Your main mistake is mixing apis. When you use mysql (which is deprecated and you mustn't use it anymore) you can't use any of mysqli functions and vice versa.
Next - as you create mysqli object with new, you should work in object-oriented style, i.e. calling methods from your mysqli object.
Try this:
<html>
<head>
<title>
Search your name by ID
</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST["searchname"])){
$id = $_POST["searchname"];
$connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
$result = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($result);
print_R($row);
}else{
echo "there is something wrong";
}
Hello im trying to delete which ever value is selected in a drop down list.
I cant seem to understand what is going on
I have 2 pages 1 with my connection and functions to view the table in a drop down (which works) and a delete function (which doesn't seem to work) and another to call the function in and to delete which ever value is selected.
connection.php
<?php
//Connect to the database
function getSQLConnection() {
$mysqlConnection = new PDO('mysql:host=localhost;dbname=isad235_100000', "root", "");
return $mysqlConnection;
}
//Get all results from members table
function getResults($tablename) {
$sql = "SELECT * FROM " . $tablename;
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
//Delete results from members table
function deleteValue($id) {
$sql = "DELETE FROM members WHERE member_id = '$id'";
$mysqlConnection = getSQLConnection();
$ResultSetting = $mysqlConnection->query($sql);
return $ResultSetting;
}
?>
delete.php
<?php
include_once 'connection.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add</title>
</head>
<body>
<h1> Delete a Member from the Members Table. </h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
Delete Member:
<select name='members' value='members'id="Mmembers">
<?php
$results = getResults('members');
if ($results) {
foreach ($results as $row) {
echo '<option value="' . $row['member_id'] . '">' . $row['name'] . '</option>';
}
}
else
echo '<option value="0"0"> No Data</option>';
?>
</select>
<input type="submit" id="delete" value="Delete"/>
<br/>
<br/>
</form>
<?php
if (isset($_POST['members'])) {
$ResultSetting = deleteValue(($_POST['members']));
}
?>
<br/>
<br/>
<form action='index.php' method='GET'>
Go Back:
<input type="submit" name="submit" value="Return"/>
</form>
<br/>
</body>
</html>
I ran your code and don't see any errors with it. Make sure the id column on your 'members' table is called 'member_id'. If there is a discrepancy in the name then the values for the option elements wouldn't be set. Also, the value you just deleted would still appear after the initial page submit. If you reload the page after the submit, you'll see the value has disappeared.