1

Why I'm getting an undefined index when declaring variables? Currently using bootstrap.

<?php 

 mysql_connect("localhost", "root", "1234") or die(mysql_error());
 mysql_select_db("newitems") or die(mysql_error());

 $up =  $_POST['update']; 
 mysql_query("UPDATE announcements SET content = $up");


 ?>
 <div class="well well-small text-center">
 <h3>Create an Announcement / Reminder:<br>

 <form class="form-group" id="form-mahasiswa" method="POST" action="ad_post.php">

  <div class="control-group">

    <div class="controls">

        <textarea id="update" name="update"></textarea><br>
        
        <button id="annbtn" class="btn btn-success">Update Announcement</button>

    </div>

</div>
3

1 Answer 1

1

The update should only take place when you submit the form. Wrap your code inside this condition:

if ($_SERVER['REQUEST_METHOD'] == POST && isset($_POST['update']) { 
   $up = $_POST['update']; 
   mysql_query("UPDATE announcements SET content = $up");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Also the mysql_* functions are deprecated so consider using the mysqli_* functions. php.net/manual/de/book.mysqli.php
And finally it's highly recommended to filter input before using in a query :-) php.net/manual/de/function.filter-input.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.