I'm trying to insert data from the form created into the SQL Server Database Table that is connected through ODBC. After I submit the data it shows up as a blank row with only the ID that has a value. I am very new to PHP and got parts of code from tutorials. This is just a test project. Here's the code:
<html>
<header>
<title>
</title>
</header>
<body>
<form action="\INSERTCODE.php" method="POST">
<input type= "number" step="any" name="sepal_lengp" placeholder="Sepal Length">
<input type= "number" step="any" name="sepal_widthp" placeholder="Sepal Width">
<input type= "number" step="any" name="petal_lengp" placeholder="Petal Length">
<input type= "number" step="any" name="petal_widthp" placeholder="Petal Width">
<input type= "text" name="flower_type" placeholder="Flower Name">
<button type="submit" name="submit" >INPUT VALUES</button>
</form>
<?php
//display all results from table
include("C:\Users\Dshop\Desktop\php-7.3.3\Server1\connection.php");
$i=1;
$sql = "SELECT * FROM dbo.textcsv";
$result = odbc_exec( $connection, $sql );
while($all =odbc_result_all($result,$i) ){
echo $all;
}
?>
</body>
</html>
This part includes the form. The filename is index1.php.
<?php
include("C:\Users\Dshop\Desktop\php-7.3.3\Server1\connection.php");
$sepal_lengp = $_POST['sepal_lengp']??'';
$sepal_widthp = $_POST['sepal_widthp']??'';
$petal_lengp = $_POST['petal_lengp']??'';
$petal_widthp = $_POST['petal_widthp']??'';
$flower_typep = $_POST['flower_typep']??'';
$dbinsert = "INSERT INTO dbo.textcsv (sepal_leng, sepal_width, petal_leng, petal_width, flower_type) VALUES ('$sepal_lengp', '$sepal_widthp', '$petal_lengp', '$petal_widthp', '$flower_typep');";
odbc_exec( $connection, $dbinsert );
HEADER("Location: ../index1.php?=success");
This part inserts data into the database table, using $_POST to obtain the data from index1.php. This file is called INSERTCODE.php. $connection and connection.php is the file that includes the connection to ODBC.
For this test project I used the Iris dataset. I believe that I had to use ODBC and SQL Server instead of mysql. Sql server is the 2014 version, PHP is 7.33, using node.js to run the server. Help is greatly appreciated!
EDIT I found out that the $_POST isn't getting any values from the form. Any ideas?
EDIT 2 I've tried using $_REQUEST, checking var_dump, and did all that stuff, but I still got nothing. After going to https://www.w3schools.com/php7/php7_forms.asp for an example form, I found out that the example did not work either. Now i'm not sure if the problem is from the code, or from something like the php configuration. Need help, please help.
you're treating your variables as strings, by th look of your database you want them as floats. Try using floatval( ) (http://php.net/manual/en/function.floatval.php) on your variables to make sure they are in the write format, this will also go some way to sanitising them until you update this to prepare statements so you can safely bind the values and specify the type
After rephrasing my issue to "node.js not taking post data" I found the issue. Node.js needs extra steps to process POST data. So, because of this, my input was ignored by node.js and the INSERTDATA.php ran without any data to insert anything. Turns out the solution of the problem was to use something like the body-parser or the other solution from another question.
The solution I took was to uninstall node.js and use XAMPP instead. It was much easier to use.
Also could someone flag my question for duplicate?
Related
I am currently trying to execute an INSERT INTO when a search button is clicked. Eventually I'd like to submit the search term into the database, however I'm currently just trying to submit a simple string to ensure the initial INSERT INTO works correctly, which it currently doesn't.
I have the following:
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="search" name="search" id="results" placeholder="Enter search">
<input type="submit" name="submit" class="frstBtn" value="searchterm"><br>
</form>
<?php
$searchterm = "";
?>
<?php
if(isset($_POST['submit']))
{
$sql = "INSERT INTO tableName (variableName) VALUES ('test')";
$results = mysqli_query($conn,$sql) or die(mysqli_error());
}
?>
The latter php function, search, works perfectly fine, but the former function including the INSERT INTO does not.
Here is the table itself in phpmyadmin.
I have successfully manually copied and pasted the actual INSERT INTO statement into the actual database and it has worked fine, so I know the issue isn't with the statement. Any help is much appreciated. Thanks.
Since the searchTerm column is defined as PRIMARY, I’m ready to bet that you are trying to insert the same value twice and you don't have enabled the error_reporting. To find out exactly what the problem is, first of all enable all errors by adding the following at the beginning of your script:
ini_set('error_reporting', -1);
ini_set('display_errors', 1);
Also, note that you must use mysqli_error($conn) instead of mysqli_error().
By the way, to ignore errors when inserting unique values, use:
INSERT IGNORE INTO searchHistory(searchTerm) VALUES ('test')
I need some help with updating query (html, php, mysql).
So I tried updating mySQL query (signature) connected with HTML. But I often face some problems.
I'd like to update a query (signature) by typing new signature in a form and a button that will submit action and change.
So basically I want to update user's signature when he types new one in a field and submits (presses a button).
If anyone can make a basic system for me so we will look forward successfully making the system.
Edit:
Yeah I know that nobody will write the script for me but I'm just out of ideas how could it be made. I'm no professional at all. I apologize for that :)
Anyway, this is the code:
<form action="update_signature.php">
<input type="text" name="txt" />
<input type="submit" name="insert" value="insert" onclick="insert()" />
</form>
I'm not sure what should I type in update_signature.php so I wrote a simple query:
<?php
$myq = mysql_query("UPDATE userSignature FROM users WHERE userSignature='$signature'")
$row=mysql_fetch_array($myq);
?> `
Thanks :)
I like how StackOverflow does not allow comments under 50 reputation...
The HTML code should have a "method", but if you skip it, it just posts as a "get", so not really a problem. In the "onclick" parameter however, you give it a JavaScript function, which does not exist?
The PHP is a mess. First, you need to retrieve the sent value with
$something = $_GET['txt'];
This puts the value what was left in the input with the name txt into a variable.
Second, the UPDATE syntax is totally different, it's more like
"UPDATE table SET column = '$phpvariable', etc... WHERE column2 = '$phpvariable2'"
where column is the name of the column in your table, and the query is successful in the lines where the column2 value is equal to the $phpvariable2.
Third, the UPDATE returns with a yes or no (success or not), not with an array.
I am new to the php & mysql scene, and am trying to input data from an html form into a sql database via php. In the long run I will want to input/modify/delete such data, so if you have any links/tutorials, etc, it would be much appreciated.
The HTML Code :
<body>
<form action="addcustomer.php" method="post">
<font size="3">
Name :
<input type="text" name="Name">
<input type="submit" value="Add Customer">
</font>
</form>
The PHP Code :
<?php
//Connecting to sql db.
$database = "my_database";
$username="user";
$password="password";
$tName="customers";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
//Sending form data to sql db.
INSERT INTO '$database'.'$tName' ('Name');
VALUES ('$_POST[Name]');
?>
CRUD (Create Read Update Delete ) is a common thing and you can find other good tutorials online.
You can check W3Schools tutorial. They Have everything you need under PHP Database section. Thanks.
Well first of all, I can suggest you reading about php and MySql. Second of all you have couple of issues here. The action post that performs on submit, puts everything in a form with a name tag in a superglobal $_POST. So you can't use this:
$database = "my_database";
$username="user";
$password="password";
$tName="customers";
As you don't have anything there. You only have one field in your html in a form with a name tag, which is this:
<input type="text" name="Name">
As I understand that this is what you are trying to achieve, to put inputs from the html form to the database.
Now in you PHP file you can read from the superglobal $_POST, like this:
$username=$_POST['Name']; //Notice that the `name=` here, is the exact name in your `html` input field
You can actually see what's inside $_POST by var dumping it: var_dump($_POST);
This is a start. See that everything is working and you getting all the relevant information from the form and then you can move to work on sending it to database.
Now for the database. You should not use mysql command instead use PDO or Mysqli.
You can find more info here on how to connect to database using PDO:
http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
and of course you can read the PHP: manual:
http://www.php.net/manual/en/intro.mysql.php
I am using Joomla 3.0 (it is up to date), and sorcerer. I have a form that I want to submit the entered data to a table in the database that I created. For some reason it will not write to the table, and I am pretty sure it can't find the database.
Since I am using sorcerer, I don't need to include code to connect to the database, it is supposed to be done automatically. I have tried countless variations of code and nothing is working. This seems like it should be pretty simple.
I am new to PHP/SQL coding, so maybe there is something wrong with my code, but after trying so many different things, I am wondering if there is another reason I can't write to the table.
Here is my HTML form and php. Essentially, I want to check if the username entered here in the form exists in the users table in my database. If it does exist and the checkbox is on, enter the username and message into the out_of_office table. If the checkbox is unchecked, delete the row from the table. But, as of now I can't even write to the table, so I figured I should get that part working properly first.
<form method="post" action="">
<p>Out of Office <input name="onoff" type="checkbox" value="ON"></p>
<p><label>Custom Out of Office Message</label>
<input type="text" name="custommessage" size="30" maxlength="25"/></p>
<p><label>Enter Username</label>
<input type="text" name="enterusername" size="30" maxlength="25"/></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
<?php
$username = $_POST['enterusername'];
$message = $_POST['custommessage'];
$query = "INSERT INTO out_of_office VALUES ('$username', '$message')";
mysql_query($query) or die ('Error');
echo "data entered";
?>
When I go to the page on my web site I get a white page that says "Error".
Like I said, I have tried a lot of code for the PHP but nothing works. Please help!
Right, rather than having all your code in an article, you should make a small module which you can then import into your article.
Here is the documentation on how to develop a basic module. In addition to this, you can also use a module generator
Once you have yourself a basic module, you're going to want to start using Joomla coding standards for getting $_POST data. Have a read of the following on how to do this:
http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
As for your database query, again, you will need to looking into specific coding standards. Read more about this here:
http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
Here is an example of what your database query would look like
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$columns = array('username', 'message');
$values = array($db->quote($username), $db->quote($message));
$query
->insert($db->quoteName('out_of_office'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->execute();
Just something to get you started off. Good luck.
Hope this helps
I am trying to give users a page depending on the windows login used. The crucial point is definitely not security, instead people want it comfortable, so they open the browser and see what they are meant to see. I thought of $_server['remote_user'] but this is empty. I googled a lot and found only things which seem to need authentication or sessions, both requiring many hours of work ;)
I am using WAMP and do not want to be more specific on the system because I want it to run tomorrow as well and I prefer the dirty solution to nothing. Grateful for your comments.
bj
Thanks to your comments I have found a viable solution at least for this special purpose:
The first file reads windows username using WScript.Network and sends a form containing the username. The second writes it to a session var. Dark and dirty but easy to maintain and handle...
File 1, session02.php:
<?php
session_start();
?>
<form id="form_user_name" name="form_new_record" action="session03.php" method="post" accept-charset="ISO-8859-1">
<input name="user_name" type="text" >
<input type="submit" value="Submit">
</form>
<script type=text/javascript>
var wscript_network=new ActiveXObject("WScript.Network");
var user_name=wscript_network.UserName;
form_user_name.user_name.value=user_name;
form_user_name.submit();
</script>
File2, session03.php:
<?php
session_start();
$current_windows_user=$_POST['user_name'];
$_SESSION['current_windows_user'] = $current_windows_user;
echo "---".$_SESSION['current_windows_user']
?>
Later either $_SESSION['current_windows_user'] or $current_windows_user are available
Thanks to everybody!
Your question is a bit unclear, if you are asking to use PHP to check a users login from a Windows PC, this can not be done.
However, if you are wanting to use a simple (little security) HTML form along with PHP and MySQL (both included with *AMP) I would suggest doing the following:
On your index.html page:
<form method="POST" action="validatelogin.php">
<input type="text" name="username" placeholder="Enter username" />
<input type="submit" value="Submit" />
</form>
On your validatelogin.php page
session_start();
$dsn = "mysql:host=localhost;dbname=example;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'root','password', $opt);
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$username = $_POST['username'];
$stmt->execute(array(
':username'=>$username,
));
$exist = $stmt->fetch();
if (!empty($exist)) {
$_SESSION['id'] = $exist["id"];
}
The last step is the most crucial:
This setup assumes you have a database named example, using the login credentails of username:root password:password. This example also assumes you have a table created inside of that database named users.
That table will have the following setup:
2 rows
first row name: id
first row type: int
first row A_I: checked
second row name: username
second row type: text
From here you can insert into the database usernames that you want people to use, I hope this helped.