if statement is running without the press of submit button in codeigniter - codeigniter-3

I want my if statement to show insert successful or failed only after I press the submit button, but as soon as I load my function master admin, is shows insert failed. What's wrong in my code.
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/layout/header',array('data'=>$data));
$this->load->view('user/layout/left_navbar',array('data'=>$data));
$this->load->view('user/appconfig/masteradmin');
$this->load->view('user/layout/footer');
$post= $this->input->post();
unset ($post['submit']);
$this->load->model('AppconfigModel');
if ($this->AppconfigModel->insert_masteradmin($post))
{
echo "insert successfull";
}
else
{
echo "insert failed";
}`

try this
if form submit and post have value that time only it will execute
$post= $this->input->post();
if($post){
unset ($post['submit']);
$this->load->model('AppconfigModel');
if ($this->AppconfigModel->insert_masteradmin($post))
{
echo "insert successfull";
}
else
{
echo "insert failed";
}
}

Related

mysqli int update syntax error

Im trying to update a field on my database but keep getting the message that my sqli code has a syntax error. I am unable to find that error, i prevously has On and Id in all caps but changed it incase it was reserved by mysqli (db.php just has my database details)
include ("db.php") ;
// connect to the database to get current state
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) { die("Connection failed: " . mysqli_connect_error());
echo "fail"; }
Also tried it as "On = 1"
$sql = "UPDATE GreenLED SET On='1' WHERE Id=1";
$result = mysqli_query($conn, $sql);
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
"On" aswell as "ON" are reserved by sql and so i had to change On to LOn

Mysqli not inserting data on table

Here's my code:
// Register User
$sql = "INSERT INTO users (username, password, email, register_date)
VALUES ('$username', '$md5pass', '$email', '$date')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
$sql = "INSERT INTO skills (user_id)
VALUES ('$last_id')" or die(mysqli_error($conn));
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Well the first table is created and everything seems ok but the second table (skills) it's not being inserted.
Can anyone tell what I'm doing wrong?
You create the second insert query, but you are not executing it.
Also, you should be checking the result of mysqli_insert_id, in case this call fails.

Creating a Follow User mechanism

I am trying to create a follow mechanism, where in a user can follow the other. What I am doing is if A tries to follow B, I insert A email id against B in follow table. Now when A again tries to follow B, A id is checked against B, if found true, it redirects and shows already followed but if A tries to follow someone whom A hasnt, it adds them.
Now the problem is, the first part is working fine, but if I follow the same person again, it gets entered again in the table.
$temp=$_GET['temp'];
echo $temp;
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."'";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
if(mysql_num_rows($data) > 0)
{
while($row=mysql_fetch_array($data))
{
$user=$row['follow_user'];
if($temp===$user)
{
header('Location:acq.php?success=1');
$_SESSION['message'] = 'Already Following.';
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}
}
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}
It looks to me as though you are adding a follow record for every user other than B that A is following. I think you might be able to fix this by restricting to B as well as A in your initial query i.e.
$query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."' and follow_user = '".$temp"'";
You can then remove the while loop:
if(mysql_num_rows($data) > 0)
{
header('Location:acq.php?success=1');
$_SESSION['message'] = 'Already Following.';
}
else
{
mysql_connect('localhost','root','');
mysql_select_db('reviewed');
$query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
$data=mysql_query($query);
if (!$data) { // add this check.
die('Invalid query: ' . mysql_error()) ;
}
echo "Success";
}

Getting error messages from Zend_Db_Adapter::exec()

I'm working on a script to make versioning our database easier. In order to do this we have a number of files (each contains at least one statement and a couple have over a hundred) containing the SQL queries that we need to update the schema that are delimited using semicolons. It works nicely 90% of the time except occasionally one of the statements will fail and we're having problems getting these fixed. When this happens we have to delete the database and manually copy and paste each SQL statement in the failing file to test it. Some queries cause an exception but for some queries the function just returns 1.
I'm using the following code but I can't figure out how to find the statement that's having the problem:
$db = Zend_Db_Table::getDefaultAdapter();
try{
// loop through all the files
foreach($files as $file){
echo 'Running ', $file, PHP_EOL;
// use the connection directly to load sql in batches
if($db->exec(file_get_contents($dir . $file)) == 1){
// *how do I get the problem line here?*
return false;
}
$db->exec('INSERT INTO database_history SET file = "' . $file .'";');
}
} catch (Exception $e) {
echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
return false;
}
Initialise a counter var like in the following example. Increment it each iteration by one. Output the counter var, when there's an error. I've called the var $line in this example. The exit terminates the script, after the var has been output. If you don't want that, you can just leave it out.
$line = 0;
foreach($files AS $file){
$line++;
// ...
if($db->exec(file_get_contents($dir . $file)) == 1){
echo '<pre>'; print_r($line); echo '</pre>'; exit;
return false;
}
// ...
}

Request dialog - How to get the sender id

I'm using this below code so that i get hte request id when my friends accepts my invite but how do i get my uid.
Say I'm A.A sends invite to b.When b ,accepts ,i can get request id but how do i get A UID
//get the request ids from the query parameter
$request_ids = explode(',', $_REQUEST['request_ids']);
//build the full_request_id from request_id and user_id
function build_full_request_id($request_id, $user_id) {
return $request_id . '_' . $user_id;
}
//for each request_id, build the full_request_id and delete request
foreach ($request_ids as $request_id)
{
echo ("reqeust_id=".$request_id."<br>");
$full_request_id = build_full_request_id($request_id, $user_id);
echo ("full_request_id=".$full_request_id."<br>");
try {
$delete_success = $facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;}
else {
echo "Delete failed".$full_request_id;}
}
catch (FacebookApiException $e) {
echo "error";}
}
When you make a GET call to /{request_id} you get back the sender and recipient uids
Try $user_id = $facebook->getUser();
edit: looking back at your code, I see you already use $user_id, so isn't that the ID you need?