Save PHP Session data across different directories - mysql

I'm trying to save PHP session data across directories. This code does not work for me and I need it to redirect to a different directory.
index.html:
<html>
<form action='login.php' method='get'>
Username: <input type='text' name='user' /><br>
Password: <input type='password' name='pass' /><br>
<input type='submit' />
</form>
</html>
login.php:
<?php
$user = $_POST['user'];
$conn = mysqli_connect('localhost', 'root' ,'a91c95n00', 'db_games2');
$sql = "SELECT username FROM userdata WHERE username='$user'";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if ($count == 0){
session_start();
$_SESSION['user'] = $user;
header('location:games.php');
}elseif ($count == 1){
header ('location:error.html');
}
?>
games.php:
<?php
session_start();
session_save_path('/var/www/html/1/');
session_save_path('/var/www/html/2/');
?>
<html>
<a href='http://76.29.204.37/1'>Product 1</a><br>
<a href='http://76.29.204.37/2'>Product 2</a>
</html>
/1/index.php
<?php
session_start();
if (!isset($_SESSION['user'])){
header ('location:error.html');
}elseif (isset($_SESSION['user'])){
header('location:success.php');
}
?>
Every time I go to the product 1 directory, it goes to error.html..
When a session is set, it is supposed to go to success.php. Please help me! Thanks!

Related

I'm creating a sign up form for an image upload but when I put the information in it says you have an error am I missing some code?

I created a signup form for an upload image, but when I enter information
in there it says you have an error, but I examined everything and I don't see a coding error. Could it be that I'm missing a good chunk of code? or I hace a coding error I'm missing a semicolon or parentheses because I don't see anything.
<?php
include_once "dbh.php";
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (first, last, username, password)
VALUES ('$first', '$last', '$uid', '$pwd')";
mysqli_query($conn, $sql);
$sql = "SELECT * FROM user WHERE username='$uid' AND first='$first'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$userid = $row['id'];
$sql = "INSERT INTO profileimg (userid, status)
VALUES ('$userid', 1)";
mysqli_query($conn, $sql);
header("Location: index.php");
}
} else {
echo "You have an error!";
}
?>
Here's The html:
if(isset($_SESSION['id'])) {
if ($_SESSION['id'] == 1) {
echo "You are logged in as user #1";
}
echo "<form action='upload.php' method='POST' enctype='multipart/form-data'>
<input type='file' name='file'>
<button type='submit' name='submit'>UPLOAD</button>
</form>";
} else {
echo "You are not logged in!";
echo "<form action='signup.php' method='POST'>
<input type='text' name='first' placeholder='First name'>
<input type='text' name='last' placeholder='Last name'>
<input type='text' name='uid' placeholder='Username'>
<input type='password' name='pwd' placeholder='Password'>
<button type='submit' name='submitSignup'>Signup</button>
</form>";
}
?>
<p>Login as user!</p>
<form action="login.php" method="POST">
<button type="submit" name="submitLogin">Login</button>
</form>
<p>Logout as user!</p>
<form action="logout.php" method="POST">
<button type="submit" name="submitLogout">Logout</button>
</form>
Screenshot of table structure
You have missed a semicolon at line
$sql = "INSERT INTO user (first, last, username, password)
and also you have put a ` in line
echo "You have an error!";
}`
I have fixed those syntax errors and if you still get some error please provide the html code from which you are taking input and make sure database connection is made properly.
Edited code::
<?php
include_once "dbh.php";
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (first, last, username, password);
VALUES ('$first', '$last', '$uid', '$pwd')";
mysqli_query($conn, $sql);
$sql = "SELECT * FROM user WHERE username='$uid' AND first='$first'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$userid = $row['id'];
$sql = "INSERT INTO profileimg (userid, status)
VALUES ('$userid', 1)";
mysqli_query($conn, $sql);
header("Location: index.php");
}
} else {
echo "You have an error!";
}
?>

Send data to the server with AJAX

I want some help with the following issue. The following code is not working for me. Is there anyone who can help me with that? I've watched this in a youtube video and i can't find why it is not running..
I want to inform you that i am running a WAMP Server at 127.0.0.1 and it is very weird that the browser does not give me any response for errors, etc..
Here is my PHP code:
<?php
$host="127.0.0.1";
$username = "root";
$password = "";
$db = "data";
$conn = mysqli_connect($host, $username, $password, $db);
if (isset($_POST['fname'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['slct'];
$query = "Insert into users (firstname, lastname,gender) Values(?,?,?))";
$stmt = $conn->prepare($query);
$stmt->bind_param('sss', $fname, $lname, $gender);
$stmt->execute();
if(mysqli_affected_rows($conn) > 0){
echo "insert";
}
else{
echo "no";
}
}
?>
Here is my html code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h3> Insert Data into Database</h3>
<form id="form1" method="post">
<p> Enter FirstName</p>
<input type="text" name="fname" class="form-control">
<br>
<p> Enter lastName</p>
<input type="text" name="lname" class="form-control">
<br>
<p>Enter Gender</p>
<select class="form-control" name="slct">
<option value="Male">Male </option>
<option value="Female">Female</option>
</select>
<br><br><br>
<button class="btn btn-primary" onclick="insertData()">Submit</button>
</form>
</body>
<script>
function insertData(){
var formData = $('#form1').serialize();
$.ajax({
url:'http://127.0.0.1/PHP/insert.php',
data:formData,
type:'Post',
success:function(response){
console.log(response);
},
error:function(err){
console.log(err);
}
})
}
</script>
</html>
Any help will be appreciate.
Thank you in advance!
Your insert-command is incorrect. Change ...(?,?,?))"; to ...(?,?,?)";
Better use an IDE like netbeans, because they would highlight such errors. Also you should switch from mysqli to PDO. And you don't need to check affected rows. The $stmt->execute() returns a boolean

Can't Update My Database

I can't update my database.. I wonder why it's not working while it worked on my other pages. I'm receiving this error:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'WHERE id =1' at line 4
This is the code:
<?php require_once("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php
$title = mysql_prep($_POST['title']);
$content = mysql_prep($_POST['content']);
$id = mysql_prep($_GET['aboutusid']);
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}',
WHERE id ={$id}";
mysql_query($query);
if(mysql_affected_rows() == 1) {
echo "Succesfully Updated {$title}
<br/>Go back to Backpanel";
} else {
echo "failed {$id}<br />".mysql_error()."<p> </p>";
}
?>
<?php require_once("include/footer.php"); ?>`
This is the form:
<?php require("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php include("include/bp_header.php"); ?>
<div id="bgcontainer">
<!-- NEWS CONTAINER -->
<div id="bodycont">
<div id="left_page">
<h2>About Us Menu</h2>
<?php list_of_aboutus(); ?>
<br />+ Add Menu
<hr />
</div>
<div id="right_page">
<h2>Edit: <?php echo $s_aboutus['title']; ?> </h2>
<br /><br />
<form action="query_editaboutus.php?aboutusid=<?php echo urlencode($s_aboutus['id']); ?>" method="post" enctype="multipart/form-data">
<table>
<tr valign="top"><td width="100px">Title:</td> <td><input name="title" type="name" size="45" value="<?php echo $s_aboutus['title']; ?>" /></td></tr>
<tr valign="top"><td width="100px">Content:</td> <td>
<textarea name="content" cols="45" rows="20" value="" > <?php echo $s_aboutus['content']; ?> </textarea>
<tr valign="top"><td width="100px">Update:</td><td><input type="submit" id="submit" value="Update" /></td></tr>
</table>
</form>
</div>
<!-- MEDIA CONTAINER -->
<?php include("include/footer.php"); ?>
Remove the comma after setting the content field.
$query = "UPDATE aboutus SET title='{$title}', content='{$content}' WHERE id ={$id}";
Original for comparison
$query = "UPDATE aboutus SET title='{$title}', content='{$content}', WHERE id ={$id}";
Check your update statement
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}'
WHERE id ={$id}";

Posting HTML form to new wp database table

I'm trying to make a HTML form post the input values to a custom table in the WordPress database. I've managed to get something to show up in a new row, but almost all of my values return N; instead of the value from the form.
Here's the code I have in my page template:
<?php
global $wpdb;
global $current_user;
$userID = $current_user->ID;
$brand = serialize($_POST["brand"]);
$url = serialize($_POST["url"]);
$sector = serialize($_POST["sector"]);
$keywords = serialize($_POST["keywords"]);
if (
'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateSearch' ) {
$ufDataUpdate = $wpdb->insert( 'wp_wct3', array(
'date' => current_time('mysql'),
'userid' => $userID,
'brand' => $brand,
'url' => $url,
'sector' => $sector,
'keywords' => $keywords ) );
}
?>
<form method="post">
<label for="brand">Brand/Product Name: </label>
<input type="text" id="brand" placeholder="eg: Spidr" class="clearfix" required />
<label for="website">Website address: </label>
<input type="url" id="url" placeholder="eg: www.spidr.co.uk" class="clearfix" required />
<label for="sector">Market sector: </label>
<input type="text" id="sector" placeholder="eg: Internet Marketing Tools" class="clearfix" required />
<label for="keyword">Keywords/Phrases:<br><span class="orange">(comma separated)</span></label>
<textarea cols="0" rows="8" class="light" id="keywords" required></textarea>
<input type="submit" id="submit" name="submit" class="button-65 mobile-button" value="release the spiders!">
<?php wp_nonce_field( 'updateSearch' ); ?>
<input name="action" type="hidden" id="action" value="updateSearch" />
</form>
Where wp_wct3 is the database name and each item in the array is the name of each column in that table.
I'm not sure if my issue lies in this code, or in the set-up of the database itself. I've used the Wordpress custom tables plugin to make the new table. The brand, url and sector simply use the text definition, while the keywords use enum('0','1').
Anyone have any ideas why the values aren't returning and I'm just getting N; ?
this is my custom-form template.. it works for me
<?php
/**
Template Name: Custom-Form
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
if (!empty($_POST)) {
global $wpdb;
$table = wp_achord;
$data = array(
'name' => $_POST['yourname'],
'chord' => $_POST['chord']
);
$format = array(
'%s',
'%s'
);
$success=$wpdb->insert( $table, $data, $format );
if($success){
echo 'data has been save' ;
}
}
else {
?>
<form method="post">
<input type="text" name="yourname">
<textarea name="chord"></textarea>
<input type="submit">
</form>
<?php } ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Figured out the main issue. I'd missed out the name attribute from the form itself, so my PHP wasn't picking up the field values!

Passing data from DB to update form using CI CRUD

I'm trying to write a compact update controller for CRUD activity. Here is the basic code:
Controller:
function update($id)
{
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('age','Age','required|is_numeric');
$this->form_validation->set_rules('country','Country','');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) {
//Failed validation or first run
$data = $this->my_model->get_record($id);
$this->load->view('myform_view', $data);
} else {
//Validation success, update DB
}
}
View:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('my_form', $attributes); ?>
<p>
<label for="name">Name</label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php echo set_value('name'); ?>" />
</p>
<p>
<label for="age">Age</label>
<?php echo form_error('age'); ?>
<br /><input id="age" type="text" name="age" value="<?php echo set_value('age'); ?>" />
</p>
<p>
<label for="country">Country</label>
<?php echo form_error('country'); ?>
<br /><input id="country" type="text" name="country" value="<?php echo set_value('country'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
This is the basic structure, however the first time the form is run there is no validated data. Therefore I have to grab this from the DB. Whats the best way to pass this to the view on the first run? And then once the form has been submitted, if validation fails then I want the failed data to show not to reload from the DB again. Whats the best way to do this?
You should have another method for the viewing aspect. Then submit your form against the "update" method. In there, you define the the form_validation as you have now.
I asked a similar question. See this link
grab the data in update controller first for edit such as
$query = $this->db->where('id',$id)->get('table_name');
$data['edit'] = $query->result_array();
and then check it in view file
value="<?php if(isset($edit[0]['age'])){echo $edit[0]['age'];}else{echo set_value('age');}?>"