如何在mysqli_fetch_array中使用extract($ row) [英] How do I use extract($row) in mysqli_fetch_array
本文介绍了如何在mysqli_fetch_array中使用extract($ row)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在user_personal.php下提取我的数据,但我不能...
这是我的代码
Register.php
<?php
session_start();
包括'db.inc.php';
$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ conn){
die(Connection failed:。mysqli_connect_error());
}
$ hobbies_list = array('Computers','Dancing','Exercise','Flying',
'Golfing',
'Hunting','Internet', '阅读','旅行','除了上市');
//过滤传入值
$ username =(isset($ _ POST ['username']))? trim($ _ POST ['username']):'';
$ password =(isset($ _ POST ['password']))? $ _POST ['password']:'';
$ first_name =(isset($ _ POST ['first_name']))? trim($ _ POST ['first_name']):'';
$ last_name =(isset($ _ POST ['last_name']))? trim($ _ POST ['last_name']):'';
$ email =(isset($ _ POST ['email']))?修剪($ _ POST ['email']):'';
$ city =(isset($ _ POST ['city']))?修剪($ _ POST ['city']):'';
$ state =(isset($ _ POST ['state']))? trim($ _ POST ['state']):'';
$ hobbies =(isset($ _ POST ['hobbies'])&& is_array($ _ POST ['hobbies']))?
$ _POST ['hobbies']:array();
if(isset($ _ POST ['submit'])&& $ _POST ['submit'] =='Register'){
$ errors = array();
//确保已输入manditory字段
if(empty($ username)){
$ errors [] ='用户名不能为空。';
}
//检查用户名是否已经注册
$ sql ='SELECT username FROM site_user WHERE username ='。
$ username。'';
$ result = mysqli_query($ conn,$ sql);
if(mysqli_num_rows($ result)> 0){
$ errors [] ='用户名'。 $ username。 '已经注册。';
$ username ='';
}
mysqli_free_result($ result);
if(空($密码)){
$ errors [] ='密码不能为空。';
}
if(empty($ first_name)){
$ errors [] ='名字不能为空。';
}
if(empty($ last_name)){
$ errors [] ='姓氏不能为空。';
}
if(空($ email)){
$ errors [] ='电子邮件地址不能为空。';
}
if(count($ errors)> 0){
echo'< p> 无法处理您的'。
'注册。 < / p> ;
echo'< p>请修正以下内容:< / p> ;
echo'< ul>';
foreach($ error as $ error){
echo'< li>'。 $错误。 < /锂> ;
}
echo'< / ul> ;
} else {
//没有错误因此请将信息输入数据库。
$ query ='INSERT INTO site_user
(user_id,username,password)
VALUES
(NULL,'。mysqli_real_escape_string($ conn,$ username)。',' 。
'PASSWORD('。mysqli_real_escape_string($ conn,$ password)。'))';
$ result = mysqli_query($ conn,$ sql);
$ user_id = mysqli_insert_id($ conn);
$ sql ='INSERT INTO site_user_info
(user_id,first_name,last_name,email,city,state,hobbies)
VALUES
('。$ user_id。','。
''。mysqli_real_escape_string($ conn,$ first_name)。','。
''。mysqli_real_escape_string($ conn,$ last_name)。','。
'' .mysqli_real_escape_string($ conn,$ email)。','。
''。mysqli_real_escape_string($ conn,$ city)。','。
''。mysqli_real_escape_string($ conn, $ state)。','。
''。mysqli_real_escape_string($ conn,join(',',$ hobbies))。'')';
if(mysqli_query($ conn,$ sql));
$ result = mysqli_query($ conn,$ sql);
$ _SESSION ['logged'] = 1;
$ _SESSION ['username'] = $ username;
header('Refresh:5; URL = main.php');
?>
< html>
< head>
< title>寄存器< /标题>
< / head>
< body>
< p> 谢谢<?php echo $ username; ?>注册! < / p>
< p>您的注册已完成!您被发送到
请求的页面。如果您的浏览器在5秒后没有正确重定向,则
< a href =main.php>点击这里< / a> 。 < / p为H.
< / body>
< / html>
<?php
die();
}
}
?>
< html>
< head>
< title>注册< / title>
< style type =text / css>
td {vertical-align:top; }
< / style>
< / head>
< body>
< form action =register.phpmethod =post>
< table>
< tr>
< td> < label for =username>用户名:< / label> < / TD>
< td> < input type =textname =usernameid =usernamesize =20maxlength =20value =<?php echo $ username;?>/> < / TD>
< / tr> < TR>
< td> < label for =password>密码:< / label> < / TD>
< td> < input type =passwordname =passwordid =passwordsize =20maxlength =20value =<?php echo $ password;?>/> < / TD>
< / tr> < TR>
< td> < label for =email>电子邮件:< / label> < / TD>
< td> < input type =textname =emailid =emailsize =20maxlength =50value =<?php echo $ email;?>/> < / TD>
< / tr> < TR>
< td> < label for =first_name>名字:< / label> < / TD>
< td> < input type =textname =first_nameid =first_namesize =20maxlength =20value =<?php echo $ first_name;?>/> < / TD>
< / tr> < TR>
< td> < label for =last_name>姓氏:< / label> < / TD>
< td> < input type =textname =last_nameid =last_namesize =20maxlength =20value =<?php echo $ last_name;?>/> < / TD>
< / tr> < TR>
< td> < label for =city>城市:< / label> < / TD>
< td> < input type =textname =cityid =citysize =20maxlength =20value =<?php echo $ city;?>/>< / td> ;
< / tr> < TR>
< td> < label for =state>州:< / label> < / TD>
< td> < input type =textname =stateid =statesize =2maxlength =2value =<?php echo $ state;?>/> < / TD>
< / tr> < TR>
< td> < label for =hobbies>爱好/兴趣:< / label> < / TD>
< td> < select name =hobbies []id =hobbiesmultiple =multiple>
<?php
foreach($ hobbies_list as $ hobby)
{
if(in_array($ hobby,$ hobbies)){
echo'< option value ='。$ hobby。'selected =selected>'。 $业余爱好。
'< / option> ;
} else {
echo'< option value ='。$ hobby。'> '。 $业余爱好。 < /选项> ;
}
}
?>
< / select> < / TD>
< / tr> < TR>
< td> < / TD>
< td> < input type =submitname =submitvalue =Register/> < / TD>
< / tr>
< / table>
< / form>
< / body>
< / html>
和我的register.php
User_personal.php
<?php
包括'auth.inc .PHP';
包括'db.inc.php';
$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ conn){
die(Connection failed:。mysqli_connect_error());
}
?>
< html>
< head>
< title>个人信息< / title>
< / head>
< body>
< h1>欢迎来到您的个人信息区。 < / H1>
< p>在这里,您可以更新您的个人信息,或删除您的
帐户。 < / p为H.
< p>您目前拥有的信息如下所示。 < / p为H.
< p> < a href =main.php>点击此处< / a>返回主页。 < / p为H.
<?php
$ sql ='SELECT
username,first_name,last_name,city,state,email,hobbies
FROM
site_user u JOIN
site_user_info i ON u.user_id = i.user_id
WHERE
username ='。mysqli_real_escape_string($ conn,$ _ SESSION ['username'])。''';;
< pre> User_personal.php
<?php
包括'auth.inc.php';
包括'db.inc.php';
$ conn = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ conn){
die(Connection failed:。mysqli_connect_error());
}
?>
< html>
< head>
< title>个人信息< / title>
< / head>
< body>
< h1>欢迎来到您的个人信息区。 < / H1>
< p>在这里,您可以更新您的个人信息,或删除您的
帐户。 < / p为H.
< p>您目前拥有的信息如下所示。 < / p为H.
< p> < a href =main.php>点击此处< / a>返回主页。 < / p为H.
<?php
$ sql ='SELECT
username,first_name,last_name,city,state,email,hobbies
FROM
site_user u JOIN
site_user_info i ON u.user_id = i.user_id
WHERE
username ='。mysqli_real_escape_string($ conn,$ _ SESSION ['username'])。''';;
$ result = mysqli_query($ conn,$ sql);
$ row = mysqli_fetch_array($ result);
extract((array)$ row);
mysqli_free_result($ result);
mysqli_close($ conn);
?>
< ul>
< li>名字:<?php echo $ first_name;?> < /锂>
< li>姓氏:<?php echo $ last_name; ?> < /锂>
< li>城市:<?php echo $ city; ?> < /锂>
< li>状态:<?php echo $ state; ?> < /锂>
< li>电子邮件:<?php echo $ email; ?> < /锂>
< li>爱好/兴趣:<?php echo $ hobbies; ?> < /锂>
< / ul>
< p> < a href =update_account.php>更新帐户< / a> |
< a href =delete_account.php>删除帐户< / a> < / p为H.
< / body>
< / html>
- 名字:<?php echo $ first_name;? >
- 姓氏:<?php echo $ last_name; ?>
- 城市:<?php echo $ city; ?>
- 状态:<?php echo $ state; ?>
- 电子邮件:<?php echo $ email; ?>
- 爱好/兴趣:<?php echo $ hobbies; ?>
我尝试了什么:
我尝试过使用echo $ row但是你没有工作
$ result = mysqli_query($ conn,$ sql);
$ row = mysqli_fetch_array($ result);
echo($ row);
mysqli_free_result($ result);
mysqli_close($ conn);
?>
< ul>
解决方案
conn = mysqli_connect(
servername,
用户名,
I want to extract my data under user_personal.php but I can not...
Here are my codes
Register.php <?php session_start(); include 'db.inc.php'; $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $hobbies_list = array('Computers', 'Dancing', 'Exercise', 'Flying', 'Golfing', 'Hunting', 'Internet', 'Reading', 'Traveling', 'Other than listed'); // filter incoming values $username = (isset($_POST['username'])) ? trim($_POST['username']) : ''; $password = (isset($_POST['password'])) ? $_POST['password'] : ''; $first_name = (isset($_POST['first_name'])) ? trim($_POST['first_name']) : ''; $last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : ''; $email = (isset($_POST['email'])) ? trim($_POST['email']) : ''; $city = (isset($_POST['city'])) ? trim($_POST['city']) : ''; $state = (isset($_POST['state'])) ? trim($_POST['state']) : ''; $hobbies = (isset($_POST['hobbies']) && is_array($_POST['hobbies'])) ? $_POST['hobbies'] : array(); if (isset($_POST['submit']) && $_POST['submit'] == 'Register') { $errors = array(); // make sure manditory fields have been entered if (empty($username)) { $errors[] = 'Username cannot be blank.'; } // check if username already is registered $sql = 'SELECT username FROM site_user WHERE username = "' . $username . '"'; $result = mysqli_query($conn,$sql); if (mysqli_num_rows ($result)> 0) { $errors[] = 'Username ' . $username . 'is already registered.'; $username = ''; } mysqli_free_result($result); if (empty($password)) { $errors[] = 'Password cannot be blank.'; } if (empty($first_name)) { $errors[] = 'First name cannot be blank.'; } if (empty($last_name)) { $errors[] = 'Last name cannot be blank.'; } if (empty($email)) { $errors[] = 'Email address cannot be blank.'; } if (count($errors) > 0) { echo '<p> Unable to process your '. 'registration. </p> '; echo '<p> Please fix the following: </p> '; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li> '; } echo ' </ul> '; } else { // No errors so enter the information into the database. $query = 'INSERT INTO site_user (user_id, username, password) VALUES (NULL, "'. mysqli_real_escape_string($conn, $username) . '", ' . 'PASSWORD("' . mysqli_real_escape_string($conn,$password) . '"))'; $result = mysqli_query($conn,$sql); $user_id = mysqli_insert_id($conn); $sql = 'INSERT INTO site_user_info (user_id, first_name, last_name, email, city, state, hobbies) VALUES (' . $user_id . ', '. '"' . mysqli_real_escape_string($conn,$first_name) . '", ' . '"' . mysqli_real_escape_string($conn,$last_name) . '", ' . '"' . mysqli_real_escape_string($conn,$email) . '", ' . '"' . mysqli_real_escape_string($conn,$city) . '", ' . '"' . mysqli_real_escape_string($conn,$state) . '", ' . '"' . mysqli_real_escape_string($conn,join(', ', $hobbies)) . '")'; if (mysqli_query($conn,$sql)); $result = mysqli_query($conn,$sql); $_SESSION['logged'] = 1; $_SESSION['username'] = $username; header('Refresh: 5; URL=main.php'); ?> <html> <head> <title> Register</title> </head> <body> <p> Thank you <?php echo $username; ?> for registering! </p> <p> Your registration is complete! You are being sent to the page you requested. If your browser doesn't redirect properly after 5 seconds, <a href="main.php"> click here </a> . </p> </body> </html> <?php die(); } } ?> <html> <head> <title> Register </title> <style type="text/css"> td { vertical-align: top; } </style> </head> <body> <form action="register.php" method="post"> <table> <tr> <td> <label for="username"> Username: </label> </td> <td> <input type="text" name="username" id="username" size="20" maxlength="20" value= "<?php echo $username; ?>"/> </td> </tr> <tr> <td> <label for="password"> Password: </label> </td> <td> <input type= "password" name= "password" id= "password" size="20" maxlength="20" value= "<?php echo $password;?>"/> </td> </tr> <tr> <td> <label for="email"> Email: </label> </td> <td> <input type="text" name= "email" id= "email" size= "20" maxlength="50" value= "<?php echo $email;?>"/> </td> </tr> <tr> <td> <label for="first_name"> First name: </label> </td> <td> <input type="text" name="first_name" id="first_name" size="20" maxlength="20" value= "<?php echo $first_name;?>"/> </td> </tr> <tr> <td> <label for="last_name"> Last name: </label> </td> <td> <input type="text" name="last_name" id="last_name" size="20" maxlength="20" value= "<?php echo $last_name;?>"/> </td> </tr > <tr> <td> <label for="city"> City: </label> </td> <td> <input type="text" name="city" id="city" size="20" maxlength="20" value= "<?php echo $city;?>"/></td> </tr> <tr> <td> <label for="state"> State: </label> </td> <td> <input type="text" name="state" id="state" size="2" maxlength="2" value= "<?php echo $state;?>"/> </td> </tr> <tr> <td> <label for="hobbies"> Hobbies/Interests: </label> </td> <td> <select name="hobbies[]" id="hobbies" multiple="multiple"> <?php foreach ($hobbies_list as $hobby) { if (in_array($hobby, $hobbies)) { echo '<option value="' . $hobby . '"selected="selected">'. $hobby . '</option> '; } else { echo '<option value="' . $hobby . '"> ' . $hobby . '</option> '; } } ?> </select> </td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value= "Register"/> </td> </tr> </table> </form> </body> </html>
And for my register.php
User_personal.php <?php include 'auth.inc.php'; include 'db.inc.php'; $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> <html> <head> <title> Personal Info </title> </head> <body> <h1> Welcome to your personal information area. </h1> <p> Here you can update your personal information, or delete your account. </p> <p> Your information as you currently have it is shown below. </p> <p> <a href="main.php"> Click here </a> to return to the home page. </p> <?php $sql = 'SELECT username, first_name, last_name, city, state, email, hobbies FROM site_user u JOIN site_user_info i ON u.user_id = i.user_id WHERE username = "' . mysqli_real_escape_string($conn,$_SESSION ['username']) . '"'; <pre>User_personal.php <?php include 'auth.inc.php'; include 'db.inc.php'; $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> <html> <head> <title> Personal Info </title> </head> <body> <h1> Welcome to your personal information area. </h1> <p> Here you can update your personal information, or delete your account. </p> <p> Your information as you currently have it is shown below. </p> <p> <a href="main.php"> Click here </a> to return to the home page. </p> <?php $sql = 'SELECT username, first_name, last_name, city, state, email, hobbies FROM site_user u JOIN site_user_info i ON u.user_id = i.user_id WHERE username = "' . mysqli_real_escape_string($conn,$_SESSION ['username']) . '"'; $result = mysqli_query($conn,$sql); $row = mysqli_fetch_array($result); extract((array)$row); mysqli_free_result($result); mysqli_close($conn); ?> <ul> <li> First Name: <?php echo $first_name;?> </li> <li> Last Name: <?php echo $last_name; ?> </li> <li> City: <?php echo $city; ?> </li> <li> State: <?php echo $state; ?> </li> <li> Email: <?php echo $email; ?> </li> <li> Hobbies/Interests: <?php echo $hobbies; ?> </li> </ul> <p> <a href="update_account.php"> Update Account </a> | <a href="delete_account.php"> Delete Account </a> </p> </body> </html>
- First Name: <?php echo $first_name;?>
- Last Name: <?php echo $last_name; ?>
- City: <?php echo $city; ?>
- State: <?php echo $state; ?>
- Email: <?php echo $email; ?>
- Hobbies/Interests: <?php echo $hobbies; ?>
Update Account |
Delete Account
What I have tried:
I have tried using echo $row but u didn't working
$result = mysqli_query($conn,$sql); $row = mysqli_fetch_array($result); echo ($row); mysqli_free_result($result); mysqli_close($conn); ?> <ul>
解决方案
conn = mysqli_connect(
servername,
username,
这篇关于如何在mysqli_fetch_array中使用extract($ row)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文