How do I insert PHP variables in a MySQL table?


2 Answers: 

$link is out of scope for your function so it does not have access to it. You will need to pass it as a parameter to your function.

add_user($link, $forename, $surname, $username, $token);
function add_user($link, $fn, $sn, $un, $pw) {
    $query = "INSERT INTO `users` (`forename`, `surname`, `username`, `password`) VALUES ('$fn', '$sn', '$un', '$pw')";
    $result = mysqli_query($link, $query);
    if (!$result) die ("Database access failed: " .mysqli_error($link));

Well, my previous poster already pointed out that $link is not in the scope of your function. I´d recommend using global statement over giving $link as a parameter to the function because its cleaner and you dont have to pass it every time.

add_user($forename, $surname, $username, $token);
function add_user($fn, $sn, $un, $pw) {
    global $link;

    $query = "INSERT INTO `users` (`forename`, `surname`, `username`, `password`) VALUES ('$fn', '$sn', '$un', '$pw')";
    $result = mysqli_query($link, $query);
    if (!$result) die ("Database access failed: " .mysqli_error());

More Articles

ubuntu - Shell command to get remote system's ip address from its mac address?

I have the HWAddress of remote machine but its IP address is not static and keeps changing. How do I get its IP address through shell?Relevant questions mostly have the remote system in the same network, but in my case its not.EDIT: I know its IP range.

Get Python command line output when called from a bash script

This may be a super simple question. I am calling a Python script on an Lubuntu OS (Cubietruck) via a shell script. This is to automate the process on start up (I want to maintain this process). Is there a simple way to view the output from Python? At the moment the Python script runs in the backgro

shell - Gmail imap can't access folders with space (or other escapted characters) in the title

Lets say i'm connection to via shell>> openssl s_client -connect -crlf>> a1 login asdasdasdasdGetting the list of the folders:>> a1 list "" *Respond:....* LIST (\HasChildren) "/" "github"* LIST (\HasNoChildren) "/" "Wizz&AKA-Ai

realtime data communication between two c++ executeables one windows and other in virtual machine ubuntu

I am running ros to control a robot using ubuntu. But the drivers are available for windows only.I have setup a virtual machine where the robot controller is built on roscppI want pass these values generated using ros to a cpp in should be realtime. data should be flowing back and forth.d

postgresql - Modifying pg_hba.conf

I want to modify the linelocal all all peertolocal all all md5within a shell script. What is the best way to go about this? If I am to search for the line, what string should I be looking for? There are a lot of spaces between each word in the pg_hba.confOr is there a better way at doing this?Thank

shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias

shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu

node.js - Not able to run my node app at port 80 on ubuntu 16.04

I want to run my node app at port 80 in ubuntu 16.04. But the moment I'm trying to run my app with npm start its throwing error Port in already in use whereas it's not in use. According to Github Reply, We need root privileges to run on ports below 1048 which is correct.So the moment I'm trying to r

ruby on rails - tmux: are my other terminals still running?

I have started using a certain technique for launching my web code since I switched server setups. I type tmuxsudo startserverrailsCtrl+B then DThen to reboot I type tmuxsudo killall rubysudo startserverrailsCtrl+B then DMy questions are: when I kill the ruby, does that end the session that I starte