Displaying a single value of an attribute using mysql_fetch_field - php

I want to fetch and display the user_id(that is auto-incremented) from my table after a user is successfully registered and his user information are stored in the same table. Here goes my code. I need help on the last few lines.
if(isset($_POST['register']))
{
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pwd=$_POST['pwd'];
$address=$_POST['address'];
$pincode=$_POST['pincode'];
$phone=$_POST['phone'];
$sql="INSERT INTO `user`(`f_name`,`m_name`,`l_name`,`email`,`password`,`address`,`pincode`,`phone`) VALUES('$fname','$mname','$lname','$email','$pwd','$address','$pincode','$phone')";
$rs=mysql_query($sql);
if($rs==1)
{
echo 'You have been registered successfully!';
}
else
{
echo "Registration failed!";
}
}
$src="SELECT `user_id` from `user` where `email`=$email";
$res=mysql_query($src);
$row=mysql_fetch_field($res);
echo $row;

echo $row[0];
$row is an array of fields, even if in your case it contains only one element. For example, if you use
SELECT a, b, c FROM ...
then $row is an array of 3 elements, $row[0] being for a, $row[1] for b, etc.

mysql_fetch_field — Get column information from a result and return as an object.
$row=mysql_fetch_field($res,0);
if(!row)
{
echo "There is no record";
}
else
{
echo $row->user_id;
}
Check Manual here

Related

Select from multiple MySQL tables

i'm trying to select value from different tables but i face some errors in the result i want to check if there is any values i will echo "Yes" otherwise echo "No"
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$username=$_POST['username'];
require_once('dbConnect.php');
$sql="SELECT donator.national_id, needy_people.national_id".
"FROM donator, needy_people".
" WHERE donator.national_id='$username' OR needy_people.national_id='$username' limit 50";
$result=mysqli_query($con,$sql);
if($check>0){
while($row=mysql_fetch_array($sql)){
$check=mysqli_fetch_array(mysqli_query($con,$sql));
}
}
if(isset($check)){
echo'YES';
}else{
echo'Noooo';
}
mysqli_close($con);
}else{
echo'error';
}
can anybody solve this problem help!
When you put as condition if($check>0), $check is not defined yet, so it won't be processed, hence $check stay unset.
Also you should use the empty() function to test existence of values in it
If you're trying to check if a row exists when you run your query consider using the function mysqli_num_rows
if (mysqli_num_rows($result) > 0) {
echo "Has row";
}
$count=mysqli_num_rows($check);
if($count>0)
echo "yes";
else
echo "no";

nested insert into two tables

I have the code below. I take with post some variables from another page. Then I run my query and I store them to the table materials. Then I select for this table in order to take materialsid and then I want with insert into to store the value of materialsid in another table called material_damages
<?php
session_start();
if(isset($_POST['submit'])) {
include('dbConfig.php');
$mname1=$_POST['name1'];
$mcost1=$_POST['cost1'];
$mquantity=$_POST['quantity'];
$res=mysql_query("INSERT INTO materials VALUES (NULL, '$mname1', '$mcost1','$mquantity')");
if ($res)
{
echo "Insert successful";
}
else
{
echo "Insert failed";
}
$res1=mysql_query("SELECT * FROM materials");
while ($row = mysql_fetch_array ($res1))
{
$id10=$row['materialsid'];
$id11=(int)$id10;
$res2=mysql_query("INSERT INTO damage_materials (damage_materials_id,damage_id,materials_id) VALUES (NULL,NULL,'$id11')");
if($res2)
{
echo "CORRECT";
}
else
{
echo "FALSE";
}
}
}
?>
The material is stored at table materials but the id does not get stored in the table damage_material. It prints Insert succesful FALSE FALSE FALSE FALSE (false is as the number of my materials)
Any ideas?

Display users First and last name in plain text from the database or give an error

I have a html page that sends value pass to log.php :
<?php
$PHONE = $_POST['pass'];
mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
mysql_real_escape_string($PASS))) or die(mysql_error()); while($row = mysql_fetch_assoc($results))
{ $rows[1] = $row; }
echo "Welcome ";
echo "$rows[1][FNAME]." ".$rows[1][LNAME]"
echo " you are logged in successfully"
?>
I get the result in log.php file in this form when value 'pass' is not being registered in database:
Welcome [//here user's first & last name if registered or remains blank] you are logged in successfully
I have table 'details' created
FNAME-First name
LNAME-Lastname
BIRTHDAY-Birthday
PHONE- user's no.
PASS- user's password.
My Question is , I want to just display ' You are not registered user' when pass value is not been registered in the database.Any help would be greatfull ?
Just check if you actually get a record or not using mysql_num_rows,
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
mysql_real_escape_string($PASS))) or die(mysql_error());
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_assoc($results))
{
$rows[1] = $row;
}
echo "Welcome ";
echo $rows[1]['FNAME']." ".$rows[1]['LNAME'];
echo "You are logged in successfully";
}
else{
//Redirect them back with error message
header("Location: http://www.example.com/index.php?err='You are not registered user'");
}
Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
You took value in $phone variable for password and in select query used PASS= $PASS so how can it run?
Change this then run like below:
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PHONE'",
mysql_real_escape_string($PHONE))) or die(mysql_error());
if(empty($rows[1][FNAME])){
echo 'You are not registered user';
}
else{
echo "Welcome ";
echo "$rows[1][FNAME]." ".$rows[1][LNAME]";
echo " you are logged in successfully";
}

Cannot echo out the right result. PHP/MySQL

I want to make a code that check if a value in an exact column is equal to an 'neodobren', and if there is such value in the column to echo out the button 'Add Member' and Yes for each value. I've tried to do the following and it is not echoing the Yes:
I have the following MySQL table content:
UID Name Phone Email SchoolGymnasium City Password Status
1 neodobren
2 neodobren
and I have the following PHP code inside the HTML index page:
$con = mysql_connect("localhost","****","****");
mysql_query("SET NAMES UTF8");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("***", $con);
include("../../sql.php");
session_start();
$zaqvki = mysql_query("SELECT * FROM Directors WHERE Status='neodobren' LIMIT 1");
if(mysql_num_rows($zaqvki) > 0) {
echo '<div align="right">Add Member</div><br>';
// display data in table
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array($zaqvki)) { //$zaqvki was $result, before a guy comment me this...
echo "Yes";
} }
else {
echo "No";
}
It is showing only the button add member, and not showing the Yes, which must be echo-ed.
Use mysql_fetch_assoc to get an associative array and make a comparison on each row of a certain column:
while($row = mysql_fetch_assoc($result))
{
if($row['Status'] === 'neodobren')
echo "Yes";
else
echo "No";
}

Not inserting where i expected

In a mysql table, i have 3 fields. user_to, user_from and id. The variables are all correct and should be inserting the correct data.
When a button is clicked, named 'poke', it should insert the cookie that stores the session of who did it and the person who was poked. It doesn't seem to be inserting and I am stuck :(
$cookie = $_SESSION['user_login'];
//Poke code
if (#$_POST['poke']) {
$check_if_poked = mysql_query("SELECT * FROM pokes WHERE user_to='$username' && user_from='$added_by'");
$num_poke_found = mysql_num_rows($check_if_poked);
if ($num_poke_found == 1) {
echo "Come on! Give the guy a chance!";
}
else
if ($username == $cookie) {
echo "You cannot Jab yourself.";
}
else
{ $poke_user = mysql_query("INSERT INTO `pokes` ('user_from', 'user_to') VALUES ('$cookie', '$username')") or trigger_error(mysql_error());
echo "$username has been jabbed.";
}
}
You used wrong quotes with fields in MySQL query.
//your wrong variant
"INSERT INTO `pokes` ('user_from', 'user_to') VALUES ('$cookie', '$username')"
//right variant
"INSERT INTO `pokes` (`user_from`, `user_to`) VALUES ('$cookie', '$username')"
Quotes like ' mean values and quotes like ` mean fields in SQL syntax
<?php
if ($_POST['poke']) {
#ref to the current user
$from = $_SESSION['user_login'];
#ref to the (poked user)
$to = $_POST['poked_user_id'];
if($from == $to){
echo "you cant poke yourself!";
}
else{
#ADVICE: USE PDO OR MYSQLI INSTEAD OF MYSQL
$check_if_poked = mysql_query("SELECT * FROM pokes WHERE user_to='$to' AND user_from='$from'");
if(mysql_num_rows($check_if_poked)){
echo "Come on! Give the guy a chance!";
}
else{
if(mysql_query("INSERT INTO `pokes` (`user_from`, `user_to`) VALUES ('$from', '$to')")){
echo "$to has been jabbed.";
}
else{
trigger_error(mysql_error());
}
}
}
}
?>
This started off as a comment - but it's getting too long to fit.
A session is not the same thing as a username - your post is very confused.
Leaving aside the wrong quotes (which is why your code is not doing what you expect)....
In a mysql table, i have 3 fields. user_to, user_from and id
... in that case you don't need to check if the row already exists - and not create duplicates. Set up a unique index then...
if (#$_POST['poke'] && ($_SESSION['user_login']!===$username)) {
$qry = "INSERT INTO `pokes` (`user_from`, `user_to`)
VALUES (".mysql_real_escape_string($_SESSION['user_login'])
.", '" . mysql_real_escape_string($username) . "')"
if (mysql_query($qry)){
echo "$username has been jabbed.";
} else if (stristr(mysql_error(), 'duplicate')) {
echo "Come on! Give the guy a chance!";
} else {
echo "It's all gone Pete Tong!";
}
} else if ($_SESSION['user_login']!===$username) {
echo "You cannot Jab yourself.";
}
While it's about the same effort for PHP processing, the DB workload is significantly less. This code also prevents some SQL injection attacks, and has error handling. I presume that $username has been created elsewhere and you don't have register_globals enabled.

Categories